omid nematollahi omid nematollahi -4 years ago 155
AngularJS Question

How to get selected value from ui-selects in ng-repeat

I'm adding ui-selects with ng-repeat but i don't know how to get value from each ui-select separately in controller. I want to get the selected item value in controller for use as parameters to send a $http request.

<div ng-repeat="repeat in repeats">
<p>Selected: {{pattern.selected.name}}</p>
<ui-select ng-model="repeat.id">
<ui-select-match placeholder="Enter an pattern...">{{$select.selected.name}}</ui-select-match>
<ui-select-choices repeat="pattern in data_pattern_newspapers track by $index">
<div ng-bind-html="pattern.name | highlight: $select.search"></div>
</ui-select-choices>





I want doing somethings as below in my controller:

alert($scope.repeat.id.selected)


How can i access to model of each ui-select in my controller?

My
plnkr

Please some body help me. thanks!

Answer Source

In your html utilise the $index property of ngRepeat to create a unique model for each ui-select, and pass $index into the ngChange function:

<ui-select ng-model="selections[$index]" ng-change="selectionChanged($index)">

In your controller initialise an array to hold the selected values, and add the above function receiving the $index to reference this array:

$scope.selections = [];

$scope.selectionChanged = function(idx) {
  console.log(idx, $scope.selections[idx]);
};

Now $scope.selections is an array of their selections as they make them.

demo: http://plnkr.co/edit/NSK3UmelATopV0Juw8BV?p=preview

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download