I want to trap on-click event of button. The id of button is dynamically generated inside the ng-repeat.
.set(ng-repeat='button in ['4', '3' , '2', '1' ]')
button(id='{{$index}}') button {{button}}
Button 4
Button 3
Button 2
Button 1
Above where do i put the my-dir
directive attribute/class?
.my-dir
.set ...
The above does not work.
and
.my-dir.set ...
executes the link function multiple times.
/* below code in greater details, if above statement is not clear enough */
Directive is
myApp.directive('myDir', function() {
var linkFn = function(scope, element, attrs) {
alert('inside linkFn');
$('button').on('click', function() { alert('button clicked');});
}
return {
restrict: 'C',
link: linkFn
}
});
using ng-repeat
the class my-dir
can be put either in the scope of ng-repeat or outside it
case 1: my-dir is outside scope of ng-repeat
.my-dir
.set(ng-repeat='button in buttons')
button(id='id{{$index}}') {{item}}
now the above code $('.set').on('click', function() { alert('button clicked');});
does not get executed!
case 2: my-dir is inside ng-repeat
(in controller) $scope.buttons=['4', '3', '2', '1']
.set(ng-repeat='button in buttons').my-dir
button(id='id{{$index}}') button {{item}}
now when you press button 4
"button clicked" shows up in alert box 4 times. For button 3
3 times and so on.