Я пытаюсь реализовать MaterialDialog, используя этот пример и по какой-то причине я получаю:
Cannot read property 'data' of null at new ACDialogComponent
Мой компонент
import { MAT_DIALOG_DATA, MatDialogRef } from "@angular/material";
import { Component, OnInit, Inject } from "@angular/core";
import { FormGroup, FormBuilder } from "@angular/forms";
@Component({
selector: 'acdialog',
templateUrl: './acdialog.component.html',
styleUrls: ['./acdialog.component.css']
})
export class ACDialogComponent implements OnInit {
form: FormGroup;
data: string="";
datatype:string;
constructor(
private fb: FormBuilder,
private dialogRef: MatDialogRef<ACDialogComponent>,
@Inject(MAT_DIALOG_DATA) d) {
this.data = d.data;
this.datatype = data.datatype;
}
ngOnInit() {
this.form = this.fb.group({
description: ["descriptionhere", []],
});
}
save() {
this.dialogRef.close(this.form.value);
}
close() {
this.dialogRef.close();
}
}
Мой HTML
<h2 mat-dialog-title>Add new contact</h2>
<mat-dialog-content [formGroup]="form">
<mat-form-field>
<input matInput placeholder="Data:" formControlName="data">
</mat-form-field>
<h4>data type</h4>
<mat-form-field>
<select matNativeControl required formControlName="datatype">
<option value="email">email</option>
<option value="phone">phone</option>
</select>
</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>
внешний компонент (тот, который открывает диалог)
constructor(private dialog: MatDialog) { }
openDialog() {
const dialogConfig = new MatDialogConfig();
dialogConfig.disableClose = true;
dialogConfig.autoFocus = true;
this.dialog.open(ACDialogComponent, dialogConfig);
const dialogRef = this.dialog.open(ACDialogComponent, dialogConfig);
dialogRef.afterClosed().subscribe(
data => console.log("Dialog output:", data)
);
}