简体   繁体   English

Angular 4/5 未捕获错误:模板解析错误:无法绑定到“mat-dialog-close”,因为它不是“button”的已知属性

[英]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有两件事你需要注意

  1. make sure you import MatDialogModule into the module where you are using the dialog.确保将MatDialogModule导入到使用对话框的模块中。
  2. make sure that the dialog component is added to declaration in your module.确保将对话框组件添加到模块中的声明中。

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.

相关问题 未捕获的错误:模板解析错误:无法绑定到“ FormGroup”,因为它不是“ form”的已知属性 - Uncaught Error: Template parse errors: Can't bind to 'FormGroup' since it isn't a known property of 'form' Angular2测试 - 失败:未捕获(在承诺中):错误:模板解析错误:无法绑定到“消息”,因为它不是已知的属性 - Angular2 Testing - Failed: Uncaught (in promise): Error: Template parse errors: Can't bind to 'message' since it isn't a known property of 未捕获错误:模板解析错误:无法绑定到&#39;ngModel&#39;,因为它不是&#39;input&#39;的已知属性 - Uncaught Error: Template parse errors: Can't bind to 'ngModel' since it isn't a known property of 'input' 未捕获(承诺):错误:模板解析错误:由于它不是“输入”的已知属性,因此无法绑定到“上载器” - Uncaught (in promise): Error: Template parse errors: Can't bind to 'uploader' since it isn't a known property of 'input' Angular中的错误:模板解析错误:无法绑定到&#39;datetime&#39;,因为它不是&#39;time&#39;的已知属性 - Error in Angular: Template parse errors: Can't bind to 'datetime' since it isn't a known property of 'time' 未捕获的错误:模板解析错误:无法绑定到“配置文件”,因为它不是“ app-trnmnt-name”的已知属性 - Uncaught Error: Template parse errors: Can't bind to 'profiles' since it isn't a known property of 'app-trnmnt-name' 未捕获的错误:模板解析错误:由于它不是&#39;app-quote-create&#39;的已知属性,因此无法绑定到&#39;quotes&#39; - Uncaught Error: Template parse errors: Can't bind to 'quotes' since it isn't a known property of 'app-quote-create' 错误:模板解析错误:无法绑定到“ ngForOf”,因为它不是“模板”的已知属性 - Error: Template parse errors: Can't bind to 'ngForOf' since it isn't a known property of 'template' Angular 6 日历模板解析错误:无法绑定到“视图”,因为它不是“div”的已知属性 - Angular 6 Calendar Template parse errors: Can't bind to 'view' since it isn't a known property of 'div' Angular 2:模板解析错误:无法绑定到&#39;ngModel&#39;,因为它不是&#39;input&#39;的已知属性 - Angular 2: Template parse errors: Can't bind to 'ngModel' since it isn't a known property of 'input'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM