angular - Angular 6 和 Material 能否在对话框中不显示整个组件,而只显示其中的一部分?
问题描述
我有一个组件和一个表格视图。同一个组件有一个用于添加新记录的表单。我想在对话框中显示此表单,但不要将其放在单独的组件中。这是可能的?
解决方案
如果我正确理解了这个问题,这是可能的。在您的组件 HTML 中使用 ng-template,其中包含您要在对话框中显示的内容。
<ng-template #dialogTemplate>
<h1 mat-dialog-title>Title</h1>
<mat-dialog-content>
Formgroup for adding item goes here
</mat-dialog-content>
<mat-dialog-actions>
<button mat-button [matDialogClose]='true' mat-icon-button><mat-icon>check</mat-icon></button>
<button mat-button [matDialogClose]='false' mat-icon-button><mat-icon>cancel</mat-icon></button>
</mat-dialog-actions>
</ng-template>
然后,在您的 ts 文件中创建一个 ViewChild
@ViewChild('dialogTemplate') dialogTemplate: TemplateRef<any>;
constructor(private dialog: MatDialog) { }
然后,您通过从按钮等调用函数来打开对话框。
open() {
const dialogRef = this.dialog.open(this.dialogTemplate);
dialogRef.afterClosed().subscribe(answer => {
if (answer === true) {
// logic to save your item goes here
}
});
}
推荐阅读
- android - 如何在android recyclerview中从服务器访问json文件的子文件和子文件
- ruby-on-rails - 在 Rails 中干燥方法调用
- php - laravel,在 forloop 中使用 updateOrCreate
- grafana - 如何在influxdb / grafana中添加多个查询的结果
- bash - vi is hashed to ( 之间有什么区别
) 和 vim 是 ? - google-apps-script - 根据带有宏/脚本的工作表的位置动态更新 rspreadsheet.getRange() 列
- javascript - 切换到新组件
- javascript - 即使在调用 Await 之后,异步函数也会返回 Promise
- python - 如何在 Redshift 数据库上从 Python 触发更新 SQL
- security - 使用基本凭据提供程序配置 Elasticsearch REST 高级客户端