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

时间:2020-10-16 18:22:11

标签: angular angular-reactive-forms

如果我将值硬编码为“ 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;
      })
    }
  }

1 个答案:

答案 0 :(得分:1)

您需要更新表单控件值:

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