获取点击元素的索引 [英] Get the index of the click element
本文介绍了获取点击元素的索引的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试获取被点击元素的索引,但我一直在控制台中获取 -1
.我正在使用 Array.prototype
从 nodelist
indexOf
HTML:
<div app-click=""><div ng-repeat="json in myJson"><li>{{json}}</li>
JS:
.directive('appClick', function() {返回 {限制:'A',范围:真实,控制器:函数($scope,$element){$element.bind("点击",function(e){var index = Array.prototype.indexOf.call($element.children(), e.target);控制台日志(索引);});}}});
解决方案
其他选项是设置索引
<div app-click=""><div ng-repeat="json in myJson"><li data-index="{{$index}}">{{json}}</li>