Created
December 20, 2012 07:17
-
-
Save jrmoran/4343498 to your computer and use it in GitHub Desktop.
AngularJS Event delegation in a repeater. SO 13965627
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]); | |
}; | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
}); | |
} | |
}; | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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