首页 文章

角度材料对话框错误'Can' t绑定到'formGroup',因为它不是't a known property of ' mat-dialog-content '.'

提问于
浏览
0

尝试按照角度材料网站上的示例添加对话框功能 . 收到错误“无法绑定到'formGroup',因为它不是'mat-dialog-content'的已知属性 . ” Angular Material文档缺少有关NgModule文件的信息 .

我错过了什么?

模块:

import { MatExpansionModule, MatDialog, MatDialogRef, MAT_DIALOG_DATA, 
        MatDialogModule } from 'node_modules/@angular/material';
import { PopupComponent } from './popup.component';
... 
@NgModule({
  imports: [
        ..
        MatDialogModule,
        ..
        ],
  declarations: [
            AppComponent,
            PopupComponent
          ],
  exports: [
         ...
        ],
  providers: [
      { provide: MAT_DIALOG_DATA, useValue: 'dialogData'}
  ],
  entryComponents: [ PopupComponent ]
  })

  export class AppModule {
}

AppComponent:

....
name: any;
animal: any;

constructor(public dialog: MatDialog) {}

openDialog(): void {
 const dialogRef = this.dialog.open(PopupComponent, {
  data: {name: this.name, animal: this.animal}
});

dialogRef.afterClosed().subscribe(result => {
  console.log(result);
});

}

AppComponent.html

<button mat-raised-button (click)="openDialog()">Pick one</button>

PopupComponent

....
export class PopupComponent implements OnInit {

 constructor(
  public dialogRef: MatDialogRef<PopupComponent>,
  @Inject(MAT_DIALOG_DATA) public data) { }

 ngOnInit() {}

 onNoClick(): void {
  this.dialogRef.close();
 }
}

PopupComponent.html

<mat-dialog-content [formGroup]="form"> 
   <mat-form-field>
     <input matInput
            placeholder="Course Description"
           formControlName="description">
     </mat-form-field>
 </mat-dialog-content>
 <mat-dialog-actions>
  <button class="mat-raised-button"(click)="close()">Close</button>
  <button class="mat-raised-button mat-primary"(click)="save()">Save</button>
</mat-dialog-actions>

1 回答

  • 0

    在你的app.module.ts中

    import {FormsModule, ReactiveFormsModule} from '@angular/forms';
    

    并添加

    FormsModule,
    ReactiveFormsModule,
    

    进口[] .

    你也在你的PopupComponent.html中绑定 [formGroup]="form" ,但我没有看到你的PopupComponent ts片段中定义的 formGroup 类型的形式 .

相关问题