选择ng-repeat选项

时间:2015-03-25 16:31:48

标签: angularjs angularjs-ng-repeat ng-repeat

我有以下脚本,

 <select ng-model="create.Category" class="form-control input-sm" name="category" required>
   <option value="" disabled="" selected="">Select Category</option>
   <option ng-repeat="cat in categories" value="{{ cat.type }}">{{cat.type}}</option>
</select>

所以我的控制器看起来像这样(类别是重新生成我的类别以填充选择选项的工厂,而info是带参数的对象实例):

 app.controller('myCtrl', function($scope, categories, info) {
    $scope.categories = categories;
    $scope.create = info;
 });

所以,我的选择选项被完美填充,但是,没有选择具有值的ng-model =“create.category”,它会显示列表中的第一个类别。

任何猜测?

2 个答案:

答案 0 :(得分:1)

首选方法是使用ngOptions:

https://docs.angularjs.org/api/ng/directive/ngOptions

答案 1 :(得分:1)

在选择中使用ng-selected并将cat.type与给定值

进行比较
<select ng-model="create.Category" class="form-control input-sm" name="category" required>
   <option value="" disabled="" selected="">Select Category</option>
   <option 
ng-selected="{{create.Category == cat.type}}"
ng-repeat="cat in categories" value="{{ cat.type }}">{{cat.type}}</option>
</select>
相关问题