I am trying to get the index of the clicked element but I keep getting -1
in the console. I am using Array.prototype
to get the indexOf
from the nodelist
HTML:
<div ng-app='app' ng-controller='mainCtrl'>
<div app-click="">
<div ng-repeat="json in myJson">
<li>{{json}}</li>
</div>
</div>
</div>
JS:
.directive('appClick', function() {
return {
restrict: 'A',
scope: true,
controller: function($scope, $element) {
$element.bind("click",function(e){
var index = Array.prototype.indexOf.call($element.children(), e.target);
console.log(index);
});
}
}
});
Other option is to set the index
<div ng-app='app' ng-controller='mainCtrl'>
<div app-click="">
<div ng-repeat="json in myJson">
<li data-index="{{$index}}">{{json}}</li>
</div>
</div>
</div>
and read it in the click
e.target.dataset.index