我在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),
有什么想法吗?
答案 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>