首页 > Angularjs select如何默认选中其中一个option

Angularjs select如何默认选中其中一个option

html代码为,如果ng-model的值在InPageSel里有的话,界面上是选中的。但是如果没有,界面上选择的是空白栏,界面下拉内容有很多,希望能做到默认选中第一个。

<select ng-model="auth_policy.p_info.high_action.action_annex.page" ng-options="en as en for en in InPageSel" style="width:150px;"></select>

js代码,select数据是从后台get过来然后放在InPageSel数组里的

$scope.InPageSel = [];
angular.forEach(data_select.script_pagelist,function(v,k){
this.push(v.page_name);
},$scope.InPageSel);

可以考虑在get之后,给auth_policy.p_info.high_action.action_annex.page一个内容里有的值


方法有很多种,个人认为最稳的方法是:
<select ng-model="selected" ng-options="x.id as x.name for x in users"></select>

js$scope.users = [
        {name:'a',id:'1'},
        {name:'b',id:'2'},
        {name:'c',id:'3'}
    ];
    $scope.selected='2';//id的值,区分类型
    $scope.selected=$scope.users[0].id;//如果想要第一个值
【热门文章】
【热门文章】