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: {{}}</p>
<ui-select ng-model="">
<ui-select-match placeholder="Enter an pattern...">{{$}}</ui-select-match>
<ui-select-choices repeat="pattern in data_pattern_newspapers track by $index">
<div ng-bind-html=" | highlight: $"></div>

I want doing somethings as below in my controller:


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


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.


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