You can actually deep dive into directive controllers and "transclusions".
To access parent controller you can use require
option.
.directive 'parent', ->
controller: ->
@addHeader = (header) => #do add header
.directive 'child', ->
require: '^parent'
link: (scope, el, attr, parent) ->
parent.addHeader 'from child'
But you need to make sure your child link function actually ran.
For example (WARNING JAVASCRIPT!!! :) you can use transclude
option. Sophisticated Example.
.directive('myTable', function() {
return {
restrict: 'E',
controller: function() {
var headers = []
this.headers = headers
this.addHeader = headers.push.bind(headers)
},
template: `
<table>
<thead>
<tr>
</tr>
</thead>
</table>
`,
transclude: {
// transclude all myHeaders into headers slot
headers: 'myHeader' // transclude (how this is a real word at all?)
},
link: function(scope, el, attrs, ctrl, transclude) {
var headerRow = el.find('thead').children('tr')
// append all headers into thead wrapping with th
transclude(function(headers) {
[].forEach.call(headers, header => {
var cell = angular.element('<th></th>')
cell.append(header)
headerRow.append(cell)
})
}, headerRow, 'headers')
console.log(ctrl.headers) // headers were populated here
}
}
})
.directive('myHeader', function() {
return {
restrict: 'E',
require: '^myTable',
transclude: true, // ohh more transclusions
template: '<span ng-transclude></span>',
link: function(scope, el, attrs, myTable) {
myTable.addHeader(attrs.name) // report to myTable
}
}
})
<my-table>
<my-header name="First"> First Header </my-header>
<my-header name="Second"> Second <span style="color:red;">Header</span> </my-header>
</my-table>