I would not recommend doing this, but you can override the ngClick directive to do what you are looking for. This does not mean what you need.
Given the initial implementation:
compile: function($element, attr) { var fn = $parse(attr[directiveName]); return function(scope, element, attr) { element.on(lowercase(name), function(event) { scope.$apply(function() { fn(scope, {$event:event}); }); }); }; }
We can do this to override it:
// Go into your config block and inject $provide. app.config(function ($provide) { // Decorate the ngClick directive. $provide.decorator('ngClickDirective', function ($delegate) { // Grab the actual directive from the returned $delegate array. var directive = $delegate[0]; // Stow away the original compile function of the ngClick directive. var origCompile = directive.compile; // Overwrite the original compile function. directive.compile = function (el, attrs) { // Apply the original compile function. origCompile.apply(this, arguments); // Return a new link function with our custom behaviour. return function (scope, el, attrs) { // Get the name of the passed in function. var fn = attrs.ngClick; el.on('click', function (event) { scope.$apply(function () { // If no property on scope matches the passed in fn, return. if (!scope[fn]) { return; } // Throw an error if we misused the new ngClick directive. if (typeof scope[fn] !== 'function') { throw new Error('Property ' + fn + ' is not a function on ' + scope); } // Call the passed in function with the event. scope[fn].call(null, event); }); }); }; }; return $delegate; }); });
You will then pass your functions as follows:
<div ng-click="func"></div>
Unlike:
<div ng-click="func()"></div>
jsBin : http://jsbin.com/piwafeke/3/edit
As I said, I would recommend not , but this is a proof of concept, showing you that yes - you can really overwrite / expand / increase the built-in angular behavior to suit your needs. Without having to dig it all in the original implementation.
Please use it with caution if you decide to go this route (this is a lot of fun).
Kasper Lewau Jul 23 '14 at 2:47 a.m. 2014-07-23 14:47
source share