Skip to content

Instantly share code, notes, and snippets.

@justindujardin
Last active May 26, 2018 16:16
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save justindujardin/19767e8ea72ccd2f41d3 to your computer and use it in GitHub Desktop.
Save justindujardin/19767e8ea72ccd2f41d3 to your computer and use it in GitHub Desktop.
AngularJS Directive require ancestor AND this controller
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();
}
};
});
<div parent-directive>
<div complex-directive></div>
</div>
@SimplGy
Copy link

SimplGy commented Aug 4, 2014

Nice. Was looking for this exact thing.

@pchiwan
Copy link

pchiwan commented Jun 4, 2015

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