A Pen by Nick LaRosa on CodePen.
Created
August 5, 2014 17:54
-
-
Save cakesmith/44d126c1ae0f4e9dc4b9 to your computer and use it in GitHub Desktop.
[AngularJS] Overriding directives with a decorator....A Pen by Nick LaRosa.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <div ng-app="codepen"> | |
| <a href="" class="blue">Not Yellow</a> | |
| <br> | |
| <a href="" class="overridden">Yellowish</a> | |
| </div> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| (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', []))); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| .blue { | |
| color: #0066FF; | |
| } | |
| .overridden { | |
| color: #FFCC00; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment