将字段绑定到反应形式不会显示值



如果我将值硬编码为类似"xxx"的值,那就可以了。但是,当我调用一个服务,然后尝试绑定该值时,不会发生任何事情。我以为它是数据绑定的,所以当设置字段时,它会显示在UI上吗?

<form [formGroup]="saveFileForm" (ngSubmit)="onExpSubmit()">
<div class="row" style="padding: 40px;">
<div class="col-md-6">
Notes:
</div>
<div class="col-md-6">
<input type="text" formControlName="Notes">
</div>
</div>
fileData: AttachmentVm = new AttachmentVm();
constructor(private service: AttachService,
private formBuilder: FormBuilder,
private router: Router,
public dialogRefSpinner: MatDialogRef<DialogRedirectToLandingSpinnerModule>,
public snackBar: MatSnackBar,
public dialog: MatDialog,
) { }
ngOnInit(): void {
this.saveFileForm = this.formBuilder.group({
PaymentDate: [''],
IsPaid: [false],
Notes: [this.fileData.GlobalNotes],
//Notes: ['xxx'],
FileDescription: ['']
});
if (this.formIdFromParent) {
this.service.getFiles(this.formIdFromParent).subscribe(result => {
this.fileData = result;
})
}
}

您需要更新表单控制值:

if (this.formIdFromParent) {
this.service.getFiles(this.formIdFromParent).subscribe(result => {
this.fileData = result;
this.formIdFromParent.controls['Notes'].setValue(this.fileData. GlobalNotes);
})
}

最新更新