One solution, which I particularly prefer, is the use of an existing function only in the policy scope, so you do not have to "fix", "hacks" or worse depend on a function of% - which goes completely against the purpose of a controller
.
Just make a definition of a function within the directive and assign it to the element by directive
, see:
.directive('acao', function() {
return {
restrict: 'A',
link: function(scope, element, attr) {
scope.meuClick = function() {
console.log('rodou');
}
}
};
});
And in your html:
<ul acao>
<li ng-repeat="item in items" ng-click="meuClick()"></li> //ngRepeat somente de exemplo
</ul>
Why use this template?
You have greater control and better maintenance over the code. Imagine that in the future you need to change the structure of ul / li to div / span, for example, or that you change the location where the policy is defined. This way you do not need to review the entire structure of the code in the directive, check if it needs a new find, etc.
You apply the click directly on the element you need and only within the policy scope, which does not interfere with the others.