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);
阅读 68.1k
3 个回答

方法有很多种,个人认为最稳的方法是:
<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;//如果想要第一个值
新手上路,请多包涵

这样好像还是有问题啊,比如默认第一个,我现在是选择第三个,结果保存还是第一个的值,还有就是修改也还是原来的默认值

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

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进