Angular 2根据条件选择选项

15

我有一个选择:

  <select id="position">
    <option *ngFor='#contactType of contactTypes' [attr.value]='contactType.contactTypeId'>
      {{contactType.description}}
    </option>
  </select>

我希望在满足条件:contactType.contactTypeId == number的情况下,不使用ngModel来选择选项。

我希望能够在没有使用ngModel的情况下,在特定条件下选择一个选项。

1个回答

35

我猜这就是你想要的:

 <select id="position">
    <option *ngFor='#contactType of contactTypes'
      [attr.value]='contactType.contactTypeId' 
      [attr.selected]="contactType.contactTypeId == number ? true : null">
      {{contactType.description}}
    </option>
  </select>
为了删除所选属性,您需要返回 null(false 会导致 selected="false")。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接