Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/29.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angular 角度-后期表单组初始化问题-未显示正确的值_Angular_Asynchronous_Angular Reactive Forms_Formgroups - Fatal编程技术网

Angular 角度-后期表单组初始化问题-未显示正确的值

Angular 角度-后期表单组初始化问题-未显示正确的值,angular,asynchronous,angular-reactive-forms,formgroups,Angular,Asynchronous,Angular Reactive Forms,Formgroups,在我的项目中,我对表单有点纠结。我有一个组件可以编辑/创建新的报价,但是这些字段是从后端异步获取的。出于某种原因,前几个字段获得默认的空值,而其余字段则正确地取自api。 我的ngOnInit: this.route.params.subscribe( (params: Params) => { this.id = params.id; this.edit = params.id != null; if (this.edi

在我的项目中,我对表单有点纠结。我有一个组件可以编辑/创建新的报价,但是这些字段是从后端异步获取的。出于某种原因,前几个字段获得默认的空值,而其余字段则正确地取自api。 我的ngOnInit:

    this.route.params.subscribe(
      (params: Params) => {
        this.id = params.id;
        this.edit = params.id != null;
        if (this.edit) {
          this.rentalService.fetchRentalById(this.id); // http request to get rental

          this.rentalService.rentalSelected.subscribe(
            rental => {
              this.editedRental = rental;
              console.log(this.editedRental); //console log shows all the data correctly
              this.initForm();
            }
          );

        } else {
          this.editedRental = new Rental();
          this.initForm();
        }
      }
    );
另外,由于某种原因,当我在浏览页面后进入页面时,只有前两个字段丢失,而当刷新页面时,前6个字段丢失

我的initForm():

为了确保我的html没有弄乱,这里有一个代码片段显示字段名称已正确初始化:

 <div class="col-xl-10">
      <input type="text" class="form-control my-3"
             id="title" placeholder="Title.."
             formControlName="title">
 </div>


我不是一个真正的前端人员,我无法发现错误,尤其是console.log()在调用initForm()之前显示了正确的数据,我希望得到一些反馈

您混合使用FormGroup构造函数和formBuilder,必须

//using new FormGroup
this.offerForm = new FormGroup({
      title: new FormControl(..)
      ...
})


嗨,谢谢你的意见。不幸的是,对于FormGroup,问题仍然存在,使用第二种方法,我得到一个错误:
error TS2349:此表达式不可调用。类型“FormBuilder”没有调用签名
//using new FormGroup
this.offerForm = new FormGroup({
      title: new FormControl(..)
      ...
})
//using formBuilder
this.offerForm = this.formBuilder({
      title: [(this.edit ? this.editedRental.title : ''), [Validators.required]]
      ...
})