[英]Angular 4/5 Uncaught Error: Template parse errors: Can't bind to 'mat-dialog-close' since it isn't a known property of 'button'
this has happened when I tried to follow the dialog tutorial from https://material.angular.io/components/dialog/overview and I'm getting the error above so if anyone has an idea of what I should do to solve this problem?当我尝试遵循https://material.angular.io/components/dialog/overview 中的对话教程时发生了这种情况,并且我收到了上面的错误,所以如果有人知道我应该怎么做来解决这个问题?
here is my code : the html for the modal这是我的代码:模态的 html
<h1 mat-dialog-title>Warning</h1>
<div mat-dialog-content>
<p>Are you sure you want to delete the book {{data.title}} ?</p>
</div>
<div mat-dialog-actions>
<button mat-button [mat-dialog-close]="data.title" tabindex="2">Ok</button>
<button mat-button (click)="onNoClick()" tabindex="-1">No Thanks</button>
</div>
the class that active the modal:激活模态的类:
import { Component, OnInit, Inject } from '@angular/core';
import { MatDialogRef, MAT_DIALOG_DATA} from '@angular/material';
@Component({
selector: 'app-remove-book',
templateUrl: './remove-book.component.html',
styleUrls: ['./remove-book.component.scss']
})
export class RemoveBookComponent implements OnInit {
constructor(
public dialogRef: MatDialogRef<RemoveBookComponent>,
@Inject(MAT_DIALOG_DATA) public data: any) { }
onNoClick(): void {
this.dialogRef.close();
}
ngOnInit() {
}
}
and the method in the class that supposes to active the modal:
removeContact(i){
let dialogRef = this.dialog.open(RemoveBookComponent, {
width: '250px',
data: { ok: this.ok, title: this.contactsArr[i].title }
});
dialogRef.afterClosed().subscribe(result => {
console.log('The dialog was closed');
this.contactsArr.splice(i,1);
});
}
I did all the required imports and it should work if someone can help I would appreciate it very much.我做了所有必需的导入,如果有人可以提供帮助,它应该可以工作,我将不胜感激。
thanks.谢谢。
好吧,在我在 git 上问它之后,我发现了我的问题,它是:导入 -> MatDialogModule 我希望它会对某人有所帮助,即使只是给出了错误的想法,请查看: https : //github.com/angular /material2/issues/8911
There are two things you need to look out for有两件事你需要注意
MatDialogModule
into the module where you are using the dialog.MatDialogModule
导入到使用对话框的模块中。Try this试试这个
<button mat-button mat-dialog-close (click)="foo(data.title)" tabindex="2">Ok</button>
instead of代替
<button mat-button [mat-dialog-close]="data.title" tabindex="2">Ok</button>
it worked for me.它对我有用。
This post should be a comment because does not supplies any answer, but to be clear where we are on this issue, this is from the Angular Material documentation: https://material.angular.io/components/dialog/overview这篇文章应该是评论,因为没有提供任何答案,但要清楚我们在这个问题上的位置,这是来自 Angular Material 文档: https : //material.angular.io/components/dialog/overview
So your code is correct as stated in Angular Material documentation, where you can find an example but, even the directives "mat-dialog-actions" and "mat-dialog-content" do not work as intended... And yes, the issue is yet opened at github.因此,您的代码如 Angular Material 文档中所述是正确的,您可以在其中找到示例,但是,即使指令“mat-dialog-actions”和“mat-dialog-content”也无法按预期工作......是的,问题尚未在 github 上打开。
Sorry for the frustration after hours of googling and SO'ing with no answer.很抱歉在经过数小时的谷歌搜索和没有答案的情况下感到沮丧。
If your component is exported in a shared module, you need to have the angular material imports declared in the shared module too.如果您的组件在共享模块中导出,则您还需要在共享模块中声明角度材料导入。
ex:前任:
import { NgModule, ModuleWithProviders } from '@angular/core'; import { MatButtonModule } from '@angular/material/button'; import { MatDialogModule } from '@angular/material/dialog'; import { ConfirmPopupComponent } from '../confirmPopup/confirmPopup.component'; @NgModule({ declarations: [ ConfirmPopupComponent ], imports: [ MatDialogModule, MatButtonModule ], exports: [ ConfirmPopupComponent ] }) export class SharedModule { }
There is an alternate option,有一个替代选项,
Dialog HTML对话框 HTML
<button mat-button mat-dialog-close (click)="myMethod('result')" cdkFocusInitial>Delete</button>
Dialog-Component.ts Dialog-Component.ts
myMethod(val){
this.dialogRef.close(val);
}
If you want to return result after dialog closed,如果您想在对话框关闭后返回结果,
Main-Component.ts主组件.ts
yourMainComponentMethod(\) {
const dialogRef = this.dialog.open(DialogDelete, {
width: '350px',
height: '200px',
data: { dialogTitle: "Title", dialogText: "You will vote this!"}
});
dialogRef.afterClosed().subscribe(result => {
console.log('The dialog was closed');
console.log(result);
});
}
use [matDialogClose]
instead of [mat-dialog-close]
.使用
[matDialogClose]
而不是[mat-dialog-close]
。 example <button mat-button [matDialogClose]="data.title" cdkFocusInitial> OK </button>
.示例
<button mat-button [matDialogClose]="data.title" cdkFocusInitial> OK </button>
。 This worked for me.这对我有用。
您还必须确保您的对话框组件在app.module.ts
声明
Did you import the MatDialogModule ?你导入了 MatDialogModule 吗? For example in your app.module.ts file
例如在您的 app.module.ts 文件中
import {MatDialogModule} from '@angular/material';
...
@NgModule({
declarations: [
...
],
imports: [
...
MatDialogModule,
],
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.