I'm trying to implement a MaterialDialog using this example and for some reason I get:
Cannot read property 'data' of null at new ACDialogComponent
My Component
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();
}
}
My 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>
the outer component (the one that opens the dialog)
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)
);
}
data-property of theconfigobject when opening the dialog with theopenfunction?MAT_DIALOG_DATAis not being injected. Do you need this in your component ?