AngularJS ng-repeat 设置默认选择值
问题描述:
$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 个值的选择框,一个空白的,然后是下拉菜单和水平栏.
Using the above I am able to create a select box with 3 values, a blank one and then dropdown menu and horizontal bar.
我想将水平栏设置为我的默认值,但不知道如何执行此操作.
I would like to set Horizontal Bar as my default and cannot see how to do this.
帮助会很棒
答
在控制器中,只需添加一行设置初始选择:
In the controller, just add a line to setup the initial selection:
$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];