I'm struggling to call a parent controller function in a nested directive that recursively compiles itself. Clicking on the button in tree-item works one level deep, but after that the variables for doc and type are undefined.
This type of issue is trivially resolved with React, but I think new scopes are being created with every function pass, when really I just want to pass the callback reference from the parent.
I think this could be solved by emitting events, is this a bad / good idea?
Parent controller method:
displayDocument(doc, type) { /* */ }
Passed down to tree directive:
<tree display-document="vm.displayDocument(doc, type)"></tree>
Tree definition:
function Tree() {
return {
restrict: 'E',
replace: true,
templateUrl: 'components/ui/biospecimen/templates/tree.html',
scope: {
displayDocument: '&',
}
}
}
Passed to tree-item directive from tree.html:
<tree-item display-document="displayDocument(doc, type)"></tree-item>
TreeItem definition:
function TreeItem($compile) {
return {
restrict: 'E',
replace: true,
templateUrl: 'components/ui/biospecimen/templates/tree-item.html',
scope: {
displayDocument: '&',
},
link: (scope, el, attrs) => {
el.append($compile([
'<tree',
'data-ng-repeat="childType in [\'foo\', \'bar\']"',
'display-document="displayDocument(doc, type)"',
'></tree>'
].join(' '))(scope))
}
}
}
And then finally used inside tree-item:
<button data-ng-click="displayDocument({ doc: doc, type: type })">Click</button>