Skip to content

Instantly share code, notes, and snippets.

@jrmoran
Created December 20, 2012 07:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jrmoran/4343498 to your computer and use it in GitHub Desktop.
Save jrmoran/4343498 to your computer and use it in GitHub Desktop.
AngularJS Event delegation in a repeater. SO 13965627
var app = angular.module('app', []);
app.controller('ctrl', function($scope){
$scope.ss = [
{name:'test1'},
{name:'test2'},
{name:'test3'},
{name:'test4'},
{name:'test5'}
];
$scope.fun = function(index){
console.log('hi from controller', index, $scope.ss[index]);
};
});
app.directive('clickChildren', function($parse){
return {
restrict: 'A',
link: function(scope, element, attrs){
var selector = attrs.selector;
var fun = $parse(attrs.clickChildren);
element.on('click', selector, function(e){
var idx = e.target.getAttribute('data-index');
fun(scope)(idx);
});
}
};
});
<body ng-controller="ctrl">
<ul click-children='fun' selector='li'>
<li ng-repeat="s in ss" data-index="{{$index}}">{{s}}</li>
</ul>
</body>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment