I have done something similar and decided to create a generalized hooking mechanism. In this way, you can define hook points in the parent controller and simply define methods on the child controller scope that will be automatically called at the appropriate point in time. You can then do whatever you want with the results from the child controllers, if need be, or simply discard them. I also discuss the tradeoffs between this approach, shared state in services, and sending events back and forth. You can see my write up here: http://sonnym.github.io/2014/02/05/hooks-in-angularjs-controllers/

- Sonny

On 02/19/2014 11:09 AM, [email protected] wrote:
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?


On Thursday, May 24, 2012 5:39:07 AM UTC+2, Vojta Jína wrote:

    You can't access child scopes from parents.
    There are reasons for that (eg. easier memory management - we can
    easily throw away a child scope, without leaking memory).

    Just do what Peter suggested. Define the property on parent scope and
    access it from child scopes. Scopes inherit prototypically.
    See this: http://jsfiddle.net/yjVD9/1/

    V.


    On Tue, May 15, 2012 at 12:20 PM, Alexander Wilms
    <[email protected] <javascript:>> wrote:
    > All, I am working on a dashboard that includes several "modules"
    with
    > different contents, e.g. users tasklist, users calendar etc. The
    dashboard
    > itself has a navigation menu and some additional features. Each
    module is
    > implemented as it's own controller and template, so they can be
    re-used in
    > other pages of the same application. So the app structure is like:
    > <MainController with navigation menu and app header>
    >  <TasklistController/> <CalendarController/> <OtherControllers/>
    > </MainController>
    > Now I'd like to some data from the "child modules" to be
    displayed in the
    > parent controller, e.g. show the number of tasks in the app
    header. While I
    > found some posts describing how to inherit data from the parent
    to the child
    > controllers I'd like to have it the other way round. How can
    that be
    > achieved?  (I am both a newbie to Angular and a designer, not a
    developers,
    > so please keep it simple :-)
    > Thanks so much,
    > Alex
    >
    > --
    > You received this message because you are subscribed to the
    Google Groups
    > "AngularJS" group.
    > To view this discussion on the web visit
    > https://groups.google.com/d/msg/angular/-/PpWzqLkzp98J
    <https://groups.google.com/d/msg/angular/-/PpWzqLkzp98J>.
    > To post to this group, send email to [email protected]
    <javascript:>.
    > To unsubscribe from this group, send email to
    > [email protected] <javascript:>.
    > For more options, visit this group at
    > http://groups.google.com/group/angular?hl=en
    <http://groups.google.com/group/angular?hl=en>.

--
You received this message because you are subscribed to the Google Groups "AngularJS" group. To unsubscribe from this group and stop receiving emails from it, send an email to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/angular.
For more options, visit https://groups.google.com/groups/opt_out.

--
You received this message because you are subscribed to the Google Groups 
"AngularJS" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/angular.
For more options, visit https://groups.google.com/groups/opt_out.

Reply via email to