In Angular 8, I have a Form group where I want to reinitialize the form with new values. But when I do fb.build the second time, the values are not displayed correctly.
The checkbox becomes non-checked even though it should be checked and the input field shows [object Object]
<form [formGroup]="myform" novalidate autocomplete="off">
<mat-checkbox
formControlName="dndcheckbox"
[(ngModel)]="userdetails.DoNotDisturb"
></mat-checkbox>
<mat-form-field>
<input matInput type="text" formControlName="fwdNumber" [(ngModel)]="userdetails.ForwardNumber" />
</mat-form-field>
</form>
userdetails: MyObject;
myform: FormGroup;
constructor(private fb: FormBuilder,) {}
ngOnInit() {
this.myService.refreshDataSubject.subscribe((data) => {
this.userdetails = data;
this.initializeForm();
});
}
initializeForm() {
this.myform = this.fb.group({
dndcheckbox: ['', ''],
fwdNumber: [
{
value: this.userdetails.ForwardNumber,
},
control => validateTelephoneNumber(this.userdetails.ForwardNumber),
],
});
}
What could be going wrong?