与ngFor循环一起动态设置属性

时间:2017-10-10 15:07:27

标签: angular

如果索引值匹配,我正在尝试动态设置selected属性。

<select class="form-control" disabled>
  <option *ngFor="let agency of request.agencyList" [attr.selected]="request.agencyIndex == agency">{{agency}}{{request.agencyIndex}}
  </option>
</select>

对象:

this.requests = [{
        agencyList: ['Agency 1', 'Agency 2', 'Agency 3'],
        agencyIndex: 1,
        ...
        }]

但是,它将所有选项selected属性设置为false

1 个答案:

答案 0 :(得分:0)

我认为这就是你要找的东西:

<select class="form-control">
   <option *ngFor="let agency of request.agencyList; let index=index;" [selected]="request.agencyIndex == index">
       {{agency}}{{request.agencyIndex}}
   </option>
</select>