angular - 在 Angular 的对话框组件中使用 [mat-dialog-close] 指令
问题描述
我使用对话框模板中的推荐代码打开对话框组件以确认或取消操作,但在执行以下按摩时遇到错误,为什么?
Property mat-dialog-close is not provided by any applicable directives nor by button element
我究竟做错了什么?
解决方案
添加app.module.ts
import {MatDialogModule} from '@angular/material/dialog';;
...
@NgModule({
declarations: [
...
],
imports: [
...
MatDialogModule,
],
import
在组件中
ts
import {Component, Inject} from '@angular/core';
import {MatDialog, MatDialogRef, MAT_DIALOG_DATA} from '@angular/material/dialog';
export interface DialogData {
animal: string;
name: string;
}
@Component({
selector: 'dialog-overview-example',
templateUrl: 'dialog-overview-example.html',
})
export class DialogOverviewExample {
animal: string;
name: string;
constructor(public dialog: MatDialog) {}
openDialog(): void {
const dialogRef = this.dialog.open(DialogOverviewExampleDialog, {
width: '250px',
data: {name: this.name, animal: this.animal}
});
dialogRef.afterClosed().subscribe(result => {
console.log('The dialog was closed');
this.animal = result;
});
}
}
@Component({
selector: 'dialog-overview-example-dialog',
template: `
<h1 mat-dialog-title>Hi {{data.name}}</h1>
<div mat-dialog-content>
<p>What's your favorite animal?</p>
<mat-form-field>
<mat-label>Favorite Animal</mat-label>
<input matInput [(ngModel)]="data.animal">
</mat-form-field>
</div>
<div mat-dialog-actions>
<button mat-button (click)="onNoClick()">No Thanks</button>
<button mat-button [mat-dialog-close]="data.animal" cdkFocusInitial>Ok</button>
</div>`,
})
export class DialogOverviewExampleDialog {
constructor(
public dialogRef: MatDialogRef<DialogOverviewExampleDialog>,
@Inject(MAT_DIALOG_DATA) public data: DialogData) {}
onNoClick(): void {
this.dialogRef.close();
}
}
html
<ol>
<li>
<mat-form-field>
<mat-label>What's your name?</mat-label>
<input matInput [(ngModel)]="name">
</mat-form-field>
</li>
<li>
<button mat-raised-button (click)="openDialog()">Pick one</button>
</li>
<li *ngIf="animal">
You chose: <i>{{animal}}</i>
</li>
</ol>
并运行服务器ng serve -o
。
推荐阅读
- aws-api-gateway - 调用 HTTP API 时通过查询参数设置 SNS 消息属性
- android-studio - 如何使我的 Android 应用程序的其他网络可以访问本地 xampp
- azure - 如何在 Azure API 管理开发人员门户中禁用注册?
- php - 如何在 CodeIgniter 的 IF 条件下加载外部 CSS 样式表
- c# - TabControl 选项卡标题文本中的额外填充
- amazon-web-services - 使用 CloudFormation 创建 CodePipeline 通知规则
- android - onResume 被调用了很多次,即使它来到前台。为什么?
- fluentd - 无法以 root 用户身份启动 td-agent
- python - 在python中传递参数的最佳实践
- c# - 单元测试服务起订量设置不返回值