Last active
May 26, 2018 16:16
-
-
Save justindujardin/19767e8ea72ccd2f41d3 to your computer and use it in GitHub Desktop.
AngularJS Directive require ancestor AND this controller
This file contains 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
var app = angular.module('yourApp'); | |
app.directive("parentDirective", function(){ | |
return { | |
restrict: "A", | |
controller:function(){ | |
this.test = function(){ | |
console.log("parentController - okay"); | |
} | |
} | |
}; | |
}); | |
app.directive("complexDirective", function(){ | |
return { | |
restrict: "A", | |
require:["complexDirective","^parentDirective"], | |
controller:function(){ | |
this.test = function(){ | |
console.log("complexController - okay"); | |
} | |
}, | |
link: function(scope,element,attributes,controllers) { | |
var thisController = controllers[0]; | |
var parentController = controllers[1]; | |
thisController.test(); | |
parentController.test(); | |
} | |
}; | |
}); |
This file contains 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 parent-directive> | |
<div complex-directive></div> | |
</div> |
This is brilliant!! Thanks for this piece of code, it saved my life! 😄
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Nice. Was looking for this exact thing.