从ng-repeat对ng-repeat项目中识别事件源

时间:2013-07-15 14:14:34

标签: angularjs

如果我有......

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

如何获得引发对update()的调用的特定选择的引用?

1 个答案:

答案 0 :(得分:10)

像这样&amp; heres小提琴:

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