Selected option in angular select

I have a choice with ng-repeat:

<select class="span5 ui-select2 id="goal_{{goal.id}}" multiple="multiple"> <option ng-repeat="counterGoal in counterGoals" value="{{counterGoal.id}}">{{counterGoal.name}}</option> </select> 

in goal , the model has an array of counter_goal_ids , for example [1,2,3,4,5,6] . How to choose options included in goal.counter_goal_ids ?

+4
source share
1 answer

Try the following:

You can select options using the ng-selected attribute and a custom function in your model

 ng-selected="isInGoalIds({{counterGoal.id}})" 

And in your model add function

 $scope.isInGoalIds = function(id){ angular.forEach($scope.counter_goal_ids, function(value, index){ if(id == value){ return true; } }); return false; } 
+9
source

Source: https://habr.com/ru/post/1442993/


All Articles