如何发送* NgFor选项的值在反应式表单中选择

时间:2019-03-21 12:38:46

标签: angular

我在Angular中有一个表单,select选项的数据来自Web api。我想知道的是,当发送的只是[对象对象]时,如何将所选值分配给表单值。 ngFor语句为:

<select class="form-select" formControlName="u_address" placeholder="Enter address">
   <option value="" disabled selected>Select your option</option>
   <option *ngFor="let item of customerAddress" [value]="item">
   {{item.address}}
   </option>
</select>

然后在component.ts文件中,它是:

  u_address: new FormControl(this.customerAddress),

有什么想法吗?

2 个答案:

答案 0 :(得分:1)

请使用ngValue代替value。

<select class="form-select" formControlName="u_address" placeholder="Enter address">
   <option value="" disabled selected>Select your option</option>
   <option *ngFor="let item of customerAddress" [ngValue]="item">
   {{item.address}}
   </option>
</select>

答案 1 :(得分:1)

只需尝试以下代码

[value]="item.address"

<select class="form-select" formControlName="u_address" placeholder="Enter address">
   <option value="" disabled selected>Select your option</option>
   <option *ngFor="let item of customerAddress" [value]="item.address">
   {{item.address}}
   </option>
</select>