How to ng:init a parent after its children?

919 views
Skip to first unread message

Eric Jain

unread,
Feb 11, 2012, 4:13:50 PM2/11/12
to AngularJS
How can I execute a method on a parent controller once, after all
controllers in child scopes have been initialized (to give them a
chance to register with the parent)?

Calling the method from an ng:init on the last child appears to work,
but perhaps there is a better way?

Vojta Jina

unread,
Feb 11, 2012, 7:44:16 PM2/11/12
to ang...@googlegroups.com
Not sure, what exactly you are asking, but you can:
- call a method of parent (as scopes inherits)
- emit an event, that will bubble up the scope hierarchy

Can you send a real use case, what you are trying to do ?

V.

Eric Jain

unread,
Feb 11, 2012, 8:49:20 PM2/11/12
to ang...@googlegroups.com

I have a dashboard-like page. Instead of having each component on this
page do it's own request to retrieve the data it needs, the components
build a single request.

Here is a simplified example:

https://gist.github.com/1805728

Vojta Jina

unread,
Feb 12, 2012, 6:04:32 PM2/12/12
to ang...@googlegroups.com
I would create a service some "batchingHttp". The controllers would ask for the data they need and this service would join all into a single request and delegate the request to $http service.

V.

Eric Jain

unread,
Feb 12, 2012, 9:18:12 PM2/12/12
to ang...@googlegroups.com

How would this "batchingHttp" service know that all controllers have
asked for the data they need and it can go ahead with the request?
Seems like the same problem?

Speaking of services: Is there any documentation on how services work in 0.10.6?

Elliott Sprehn

unread,
Feb 13, 2012, 12:25:53 AM2/13/12
to ang...@googlegroups.com

On Feb 12, 2012, at 6:18 PM, Eric Jain wrote:

> On Sun, Feb 12, 2012 at 15:04, Vojta Jina <vojta...@gmail.com> wrote:
>> I would create a service some "batchingHttp". The controllers would ask for
>> the data they need and this service would join all into a single request and
>> delegate the request to $http service.
>
> How would this "batchingHttp" service know that all controllers have
> asked for the data they need and it can go ahead with the request?
> Seems like the same problem?

Digests happen synchronously so you just need to $defer/setTimeout a callback that executes the batched request and then every controller in the current cycle gets a chance to add something to the batch.

Eric Jain

unread,
Feb 13, 2012, 12:50:07 AM2/13/12
to ang...@googlegroups.com
On Sun, Feb 12, 2012 at 21:25, Elliott Sprehn <esp...@gmail.com> wrote:
> Digests happen synchronously so you just need to $defer/setTimeout a callback that executes the batched request and then every controller in the current cycle gets a chance to add something to the batch.

Using $defer appears to work. What's a digest?

Vojta Jina

unread,
Feb 13, 2012, 1:16:14 AM2/13/12
to ang...@googlegroups.com
$digest is a phase when angular checks all the watched expressions, basically everything within angular happens inside a $digest cycle.

Btw, using scope.$evalAsync() might be better than $defer, as it executes before leaving the js context, but as long as you only fire the xhr requests, it's no a big deal...

V.

Eric Jain

unread,
Feb 13, 2012, 1:40:27 AM2/13/12
to ang...@googlegroups.com
On Sun, Feb 12, 2012 at 22:16, Vojta Jina <vojta...@gmail.com> wrote:
> Btw, using scope.$evalAsync() might be better than $defer, as it executes
> before leaving the js context, but as long as you only fire the xhr
> requests, it's no a big deal...

How do I get hold of the "scope"?

I tried $rootScope.$evalAsync(f), and that appears to work as well...

Vojta Jina

unread,
Feb 13, 2012, 1:57:08 AM2/13/12
to ang...@googlegroups.com
Until 0.10.7, scope is a controller, so you can do this in your controller:
this.$evalAsync(....)

Since 0.10.7, we inject scope into controller...
function MyCtrl($scope) {
  $scope.$evalAsync(...);
}

V.
Reply all
Reply to author
Forward
0 new messages