andresmijares
andresmijares

Reputation: 3744

selected option into ng-repeat

I'm having the following script,

 <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>

so my controller looks like this (categories is a factory which reaturn my categories to fill the select options, and info is an object instance with the parameters):

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

so, my select options are filled perfectly, however, the ng-model="create.category" which has a value, is not been selected, it display the first category on the list.

Any guessing?

Upvotes: 0

Views: 7892

Answers (2)

A.B
A.B

Reputation: 20445

use ng-selected in select and compare the cat.type with value given

<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>

Upvotes: 1

sirrocco
sirrocco

Reputation: 8055

The preferred way is to use ngOptions :

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

Upvotes: 0

Related Questions