I am trying to figure out how to properly use nested directives with transclusion and ^require in angular. I want to have a outter directive have a variable that gets updated by nested children directives, but I want all of the children to be linked to that variable. I have written a very simple example to demonstrate the problem
JS
(function () {
'use strict';
angular
.module('app')
.directive('test', test);
function test() {
var directive = {
bindToController: true,
controller: testController,
'controllerAs': 'testController',
scope: {},
templateUrl: 'scripts/test/test.html',
transclude: true
};
return directive;
}
function testController() {
var self = this;
self.childCount = 0;
self.addChild = function addChild(child) {
self.childCount++;
child.childNumber = self.childCount;
}
}
})();
(function () {
'use strict';
angular
.module('app')
.directive('child', child);
function child() {
var directive = {
'scope': {},
'link': link,
'templateUrl': 'scripts/test/child.html',
'transclude': true,
'require': '^test'
};
return directive;
function link(scope, element, attrs, testController) {
scope.childNumber = null;
testController.addChild(scope);
}
}
})();
Main HTML calls
<test>
<child></child>
<child></child>
<child></child>
</test>
test.html partial
<h1>self.childCount = {{testController.childCount}}</h1>
<div ng-transclude></div>
child.html partial
<h3>I am child {{childNumber}} out of {{testController.childCount}}</h3>
Output (and issue)
self.childCount = 3
I am child 1 out of
I am child 2 out of
I am child 3 out of
As you can see, the child.html output does not know how to output {{testController.childCount}}. Any ideas on what is going wrong?