Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Identify event source from ng-change on ng-repeat item

Tags:

angularjs

If I have....

<ul>
   <li ng-repeat="s in collection">
     <select ng-change="update()">
        <option></option>
     </select>
   </li>
</ul>

How do I get a reference to the specific select that raises a call to update()?

like image 879
Ian Warburton Avatar asked Jan 26 '26 18:01

Ian Warburton


1 Answers

Like this & heres the fiddle:

<div ng-controller="MyCtrl">
  <ul>
   <li ng-repeat="s in collection">
     Hello I am {{s.name}} & these are my options
     <select ng-options="o as o for o in s.options" ng-model="s.selectedOption" ng-change="optionChanged()">
     </select>
   </li>
</ul>
</div>

<script>
var myApp = angular.module('myApp',[]);

function MyCtrl($scope) {
    $scope.collection = [
        {'name':'one', 'value':1, 'options':['A','B']},
        {'name':'two', 'value':2, 'options':['C','D']}
    ];

    $scope.optionChanged = function(){
        console.debug(this.s.selectedOption);
    }
}
</script>
like image 171
marko Avatar answered Jan 28 '26 11:01

marko



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!