本文介绍了Angular 2 下拉选项默认值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在 Angular 1 中,我可以使用以下命令为下拉框选择默认选项:
In Angular 1 I could select the default option for a drop down box using the following:
<select
data-ng-model="carSelection"
data-ng-options = "x.make for x in cars" data-ng-selected="$first">
</select>
在 Angular 2 中,我有:
In Angular 2 I have:
<select class="form-control" [(ngModel)]="selectedWorkout" (ngModelChange)="updateWorkout($event)">
<option *ngFor="#workout of workouts">{{workout.name}}</option>
</select>
考虑到我的选项数据,我如何选择默认选项:
How could I select a default option given my option data is:
[{name: 'arm'}, {name: 'back'}, {name:'leg'}]
而我默认的值是 back代码>?
推荐答案
向 selected
属性添加绑定,如下所示:
Add a binding to the selected
property, like this:
<option *ngFor="#workout of workouts"
[selected]="workout.name == 'back'">{{workout.name}}</option>
这篇关于Angular 2 下拉选项默认值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!