I have this Review Dialog component :
export class ReviewDialogComponent implements OnInit {
form: FormGroup;
comments: string;
rating: number;
constructor(
private fb: FormBuilder,
private dialogRef: MatDialogRef<ReviewDialogComponent>,
@Inject(MAT_DIALOG_DATA) data) {
this.comments = data.comments;
this.rating = data.rating;
this.form = fb.group({
comments: [this.comments, []],
rating: [this.rating, []]
});
}
ngOnInit() {
}
save() {
this.dialogRef.close(this.form.value);
}
close() {
this.dialogRef.close();
}
}
The html :
<mat-dialog-content [formGroup]="form">
<mat-form-field>
<textarea matInput
placeholder="Comments"
formControlName="comments">
</textarea>
</mat-form-field>
<mat-form-field>
<mat-select placeholder="Select rating"
formControlName="rating">
<mat-option value="1">1</mat-option>
<mat-option value="2">2</mat-option>
<mat-option value="3">3</mat-option>
<mat-option value="4">4</mat-option>
<mat-option value="5">5</mat-option>
</mat-select>
</mat-form-field>
</mat-dialog-content>
And usage :
addReview() {
const dialogConfig = new MatDialogConfig();
dialogConfig.disableClose = true;
dialogConfig.autoFocus = true;
dialogConfig.data = {
comments: '',
rating: null
};
const dialogRef = this.dialog.open(ReviewDialogComponent,
dialogConfig);
dialogRef.afterClosed().subscribe(
val => console.log("Dialog output:", val)
);
}
I have all the imports needed, it loads the dialog but it returns the error after a few seconds. I've tried adding ngDefaultControl but after that I receive "Error: mat-form-field must contain a MatFormFieldControl." so it doesn't really help. Also, I've already checked angular2 rc.5 custom input, No value accessor for form control with unspecified name, didn't help. What am I missing ?
Thanks a lot !