Angularjs将数组的索引传递给ng-change函数

angularjs pass index of array into ng-change function

本文关键字:ng-change 函数 索引 数组 Angularjs      更新时间:2023-09-26

我使用ng-select来填充我的选项,但是我想传递选定的索引,在ng-change函数中应该有什么?

就像如果用户选择了b,我将得到1,这是数组的第二个索引。

http://jsfiddle.net/kqcrL5j3/

<select 
            ng-options="p as a for a in alpa"
            ng-model="selected" ng-change="detectChange('what to pass index here?')"></select>

像这样修改代码,

<select ng-options="a for a in alpa" ng-model="selected" ng-init="selected='a'" ng-change="detectChange(selected)"></select>

您可以传递整个选定对象并查找索引,如

  $scope.detectChange = function(selected_index){
       var indexno =  $scope.alpa.indexOf(selected_index );
       console.log(indexno)
    }