如何设置Angularjs2 Beta中选择的下拉列表项

时间:2016-02-18 08:11:25

标签: typescript angular

我正在使用angularjs 2和typescript,其中我有一个动态加载的下拉列表。

 <select name="" tabindex="1">
    <option *ngFor="#option of options" value="{{option.value}}" > 
        {{option.text}} 
    </option>
 </select>

这是我的数据

options: option[] = [
        { text: 'Deny access to this web address',value: 1 },
        { text: 'Sample banned content list2', value: 2},
        { text: 'Sample banned content list3', value: 3}
    ];

如何将选项[0]设置为&#39;已选择&#39; (<option value='1' selected>Deny access to this web address </option>)这是预期的输出。我该怎么设置呢?

1 个答案:

答案 0 :(得分:1)

  <select name="" tabindex="1">
    <option *ngFor="#option of options; #i=index" value="{{option.value}}" [selected]="i == 0 ? true : null" > 
        {{option.text}} 
    </option>
  </select>

Plunker