Chapter 1: Interaction between nested directives

by billy roberts

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

//a child calls into the controller interface of
// 1 it's sibling, and 2 it's parent.
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'],
        link: function (scope, el, attrs, ctrls) {
            ctrls[0].identify();
            // Parent!
            ctrls[1].identify();
            // Sibling!
        }
    };
});