预期行为
当所选值设置为在选择下拉列表中设置该选项时
HTML代码:
<select id="products" class="form-control new-select padding" formControlName="featured_product">
<option>Please Select</option>
<option *ngFor="let p of products" [ngValue]="p" [attr.selected]="p.ProductId == product.ProductId ? '': null" >{{p.ProductName}}</option>
</select>
答案 0 :(得分:0)
<select id="products" class="form-control new-select padding" formControlName="featured_product">
<option>Please Select</option>
<option *ngFor="let p of products" [value]="p" [attr.selected]="p.ProductId == product.ProductId ? '': null" >{{p.ProductName}}</option>
</select>
我已经尝试过这段代码并且工作正常。
products = [{ProductId: 1, ProductName : 'a'},
{ProductId: 2, ProductName : 'ab'},
{ProductId: 3, ProductName : 'abc'},
{ProductId: 4, ProductName : 'abcd'} ];
product = {ProductId: 2, ProductName : 'ab'};