I want to override directive ng-click: to some make some $rootscope changes before each execution of ng-click. How to do it?
AngularJS allows you to create custom directives with which it becomes easier to encapsulate and simplify DOM manipulation in AngularJS. These directives extend the HTML functionality.
AngularJS directives are extended HTML attributes with the prefix ng- . The ng-app directive initializes an AngularJS application. The ng-init directive initializes application data. The ng-model directive binds the value of HTML controls (input, select, textarea) to application data.
Every directive is a special service inside AngularJS, you can override or modify any service in AngularJS, including directive
For example remove built-in ngClick
angular.module('yourmodule',[]).config(function($provide){ $provide.decorator('ngClickDirective', ['$delegate', function($delegate) { //$delegate is array of all ng-click directive //in this case first one is angular buildin ng-click //so we remove it. $delegate.shift(); return $delegate; }]); });
angular support multiple directives to the same name so you can register you own ngClick
Directive
angular.module('yourmodule',[]).directive('ngClick',function (){ return { restrict : 'A', replace : false, link : function(scope,el,attrs){ el.bind('click',function(e){ alert('do you feeling lucky'); }); } } });
check out http://plnkr.co/edit/U2nlcA?p=preview I wrote a sample that removed angular built-in ng-click
and add a customized ngClick
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With