Chapter 1: Optional nested directive controllers
HTML
<script src="https://code.angularjs.org/1.3.2/angular.min.js"></script>
<div ng-app="myApp">
<div parent-directive>
<div child-directive sibling-directive></div>
</div>
</div>
JavaScript
angular.module('myApp', [])
.directive('parentDirective', function ($log) {
return {
controller: function () {
this.identify = function () {
$log.log('Parent!');
};
}
};
})
.directive('siblingDirective', function ($log) {
return {
controller: function () {
this.identify = function () {
$log.log('Sibling!');
};
}
};
})
.directive('childDirective', function ($log) {
return {
require: [
'^parentDirective',
'^siblingDirective',
'^?missingDirective'
],
link: function (scope, el, attrs, ctrls) {
ctrls[0].identify();
// Parent!
ctrls[1].identify();
// Sibling!
$log.log(ctrls[2]);
// null
}
};
});