angular.js select doesn't show the option value in the begining

走远了吗. 提交于 2019-12-14 03:12:59

问题


I am using angular.js and bootstarp - angular-ui-bootstarp. and I found a problem that when using the select, even the option is not empty, the select in the page always show nothing until you click the option list. The first thing I tried is directly put options in the select, and make one option with ng-selected="selected", but no working

 <select class=" input-sm pull-right" style="margin-right: 5px;" ng-change="selectUserFilter()" ng-model="user_filter_key" >
  <option >All</option>
  <option ng-selected="selected">Active</option>
 </select>

The second thing I tried is to embedded a model with options list into the select, still not working

  $scope.user_options = ['All','Active'];
 <select class=" input-sm pull-right" style="margin-right: 5px;" ng-change="selectUserFilter()" ng-model="user_filter_key" ng-options="opt for opt in user_options">
     </select>

回答1:


Please see here http://jsbin.com/tifur/1/edit

just set user_filter_key in yyour controler by :

 $scope.user_filter_key = $scope.user_options[0]

html:

<body ng-app="app">
  <div ng-controller="firstCtrl">
    <select class=" input-sm pull-right" style="margin-right: 5px;" ng-change="selectUserFilter()" ng-model="user_filter_key" ng-options="opt for opt in user_options">
    </select>
  </div>
</body>

JS:

var app = angular.module('app', []);

app.controller('firstCtrl', function($scope) {
  $scope.user_options = ['All', 'Active'];


  //add this
  $scope.user_filter_key = $scope.user_options[0]



});


来源:https://stackoverflow.com/questions/26190936/angular-js-select-doesnt-show-the-option-value-in-the-begining

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!