Skip to content

Instantly share code, notes, and snippets.

@cakesmith
Created August 5, 2014 17:54
Show Gist options
  • Select an option

  • Save cakesmith/44d126c1ae0f4e9dc4b9 to your computer and use it in GitHub Desktop.

Select an option

Save cakesmith/44d126c1ae0f4e9dc4b9 to your computer and use it in GitHub Desktop.
[AngularJS] Overriding directives with a decorator....A Pen by Nick LaRosa.
<div ng-app="codepen">
<a href="" class="blue">Not Yellow</a>
<br>
<a href="" class="overridden">Yellowish</a>
</div>
(function(pen) {
pen.config(function($provide) {
$provide.decorator('aDirective', function($delegate) {
// can also use function($delegate, anotherDirective) to inject a directive dependency delegate
// (say that three times fast.)
var directive = $delegate[0];
directive.compile = function(element, attrs) {
// here we can modify the compile function, or call another directive's compile or link functions
attrs.$addClass('overridden');
// if there is a link function to override or extend, we can call it here with:
//
// var link = directive.link;
//
// compile(element, attrs);
// return function(scope, element, attrs) {
//
// do something here
// return link(scope, element, attrs);
// }
return compile(element, attrs);
};
return $delegate;
});
});
}(angular.module('codepen', [])));
.blue {
color: #0066FF;
}
.overridden {
color: #FFCC00;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment