$scope.activities =
[
{ id: 1, type: "DROPDOWN_MENU", name: "Dropdown Menu" },
{ id: 2, type: "HORIZONTAL_BAR", name: "Horizontal Bar" }
];
<select data-ng-model="engineer.currentActivity" data-ng-options="a.name for a in activities">
</select>
使用上面我可以创建一个包含3个值的选择框,一个空白,然后是下拉菜单和水平条。
我想将Horizontal Bar设置为默认值,但无法看到如何执行此操作。
帮助会很棒
答案 0 :(得分:4)
在控制器中,只需添加一行即可设置初始选择:
$scope.activities =
[
{ id: 1, type: "DROPDOWN_MENU", name: "Dropdown Menu" },
{ id: 2, type: "HORIZONTAL_BAR", name: "Horizontal Bar" }
];
$scope.engineer = {}; // if needed
$scope.engineer.currentActivity = $scope.activities[1];
答案 1 :(得分:3)
在角度控制器中:
$scope.activities = [
{ id: 1, type: "DROPDOWN_MENU", name: "Dropdown Menu" },
{ id: 2, type: "HORIZONTAL_BAR", name: "Horizontal Bar" }
];
$scope.activity = $scope.activities[1]; //Bind 2nd activity to the model.
在HTML中:
<select ng-model="activity"
ng-options="activity as activity.name for activity in activities">
<option value=""></option>
</select>
请参阅Fiddle
答案 2 :(得分:0)
使用ng-init
directive
ng-init="engineer.currentActivity = options[1]"