当前位置 : 主页 > 手机开发 > 其它 >

角度 – 在依赖模型驱动形式的同时预先填充ng-bootstrap的预先输出的问题

来源:互联网 收集:自由互联 发布时间:2021-06-22
假设我有以下模型驱动形式: this.addressForm = this.formBuilder.group({ address: this.formBuilder.group({ placeId: [this.address.placeId], description: [this.address.description] })}); 以下模板: form [formGroup]="addressFo
假设我有以下模型驱动形式:

this.addressForm = this.formBuilder.group({
  address: this.formBuilder.group({
    placeId: [this.address.placeId],
    description: [this.address.description]
  })
});

以下模板:

<form [formGroup]="addressForm" (ngSubmit)="updateAddress()" novalidate>
  <div class="form-group">
    <div class="input-group">
      <input type="text"
         formControlName="address"
         placeholder="Type in you address"
         [ngbTypeahead]="chooseAddress"
         [inputFormatter]="addressFormatter"
         [resultFormatter]="addressFormatter"
         autocomplete="off"
         class="form-control">
    </div>
   ...
</form>

addressFormatter:

addressFormatter = param => param.description;

假设地址是一个具有两个属性的对象:placeId和description.

似乎不可能处理formGroup(这里是地址)而不是formControl(这里是address.placeId),并且仍然使用对象的属性之一(例如address.description)填充表单.

我收到以下错误:

Error in ./UserAccountAddressComponent class UserAccountAddressComponent – inline template:8:9 caused by: control.registerOnChange is not a function
TypeError: control.registerOnChange is not a function

我无法在字段中显示对象的一个​​属性(address.description),并在提交表单(address.placeId)时使用另一个属性,同时仍然能够使用其中一个对象属性预填充表单(此处) address.description).

有人可以帮忙吗?

我猜您的错误意味着与您的输入绑定的组件未实现 ControlValueAccessor interface.尝试将[formControl]绑定添加到您的输入:

<form [formGroup]="addressForm" (ngSubmit)="updateAddress()" novalidate>
  <div class="form-group">
    <div class="input-group">
      <input type="text"
         formControlName="address"
         [formControl]="addressForm.address"
         placeholder="Type in you address"
         [ngbTypeahead]="chooseAddress"
         [inputFormatter]="addressFormatter"
         [resultFormatter]="addressFormatter"
         autocomplete="off"
         class="form-control">
    </div>
   ...
</form>
网友评论