I'm creating a directive that needs to interact with children directives generated through a template.
<div my-parent="config" />
config is an object that defines:
- the parent model
- his controller (eg. AnnoyngParent_Ctrl) that extends BaseParent_Ctrl (and optionally a set of decorators)
- a template e.g.
<h1>parent.title</h1>
<ng-repeat="child in parent.children" my-child="child" />
- a list of children. Each child has a controller (eg. LazyChild_Ctrl), a model and a template
In both directives link functions I do add the controller and I compile the template.
It's quite easy to have children communicate with their parent (scope inheritance and $emit in the child controller, require "^myParent" + controller in the link phase of the directive).
Having the parent interact with their children is not as easy. $broadcast is an option for generic commands, not for more complex flows where at some point the parent needs to interact with a single child and do some conditional operation.
The second option we evaluated was using a shared service (or a combination of service and broadcast) but this makes the flow control cumbersome.
The last one we come through is to have the children "register" to the parent controller ( $scope.registerAsChild(this); ). While this seams a bad approach I've seen that something similar is used in the ngSwitch-ngSwitchWhen directives.
Is there any flaw in this approach?
Any alternative solution?