$on('$viewContentLoaded') is never executed on custom directive

3,962 views
Skip to first unread message

Noe Nieto

unread,
Dec 5, 2012, 5:01:48 PM12/5/12
to ang...@googlegroups.com
Hi. I wrote a custom directive in order to use a jquery gallery plugin. The idea is to use $http to retrieve the data, then use ng-repeat to fill the template and finally execute the jquery plugin.

The jsfiddle is here: http://jsfiddle.net/tzicatl/kYRN9/10/

The first problem is that the callback for $on('$viewContentLoaded') is never executed. How do I write a directive that waits for the page  to finish loading just like $(document).ready() does?

--
Noe

Daniel Szak Ferenc

unread,
Dec 6, 2012, 6:02:01 AM12/6/12
to ang...@googlegroups.com
Noe,

since that $viewContentLoaded event is not part of angular (is it?), You have to emit it Yourself, whenever You think it's the time to do so.

As You "simulate" an $http request, i imagine that You want to fire Your custom event when that "loading" is done.

I hope this is what You were looking for...

Cheers and happy coding!

Paul Dijou

unread,
Dec 6, 2012, 7:46:31 AM12/6/12
to ang...@googlegroups.com
Daniel: it is, see http://docs.angularjs.org/api/ng.directive:ngView

Noe: this event is emitted by the ng-view directive, and there is no ng-view at all in your jsFiddle, so no chance to have that event for now. In top of that, the event will be emitted, which means it will go up in your DOM and never reach your directives which are inside the ng-view. A workaround for that is to attach a $on listener on the $rootScope and when it receive the event,  it broadcast it back so it would go down your DOM.

Daniel Ferenc Szak

unread,
Dec 6, 2012, 7:51:40 AM12/6/12
to ang...@googlegroups.com
Paul, cool!

The search input at the docs did not show results for "$viewContentLoaded"...

Nice to know!


--
You received this message because you are subscribed to the Google Groups "AngularJS" group.
To post to this group, send email to ang...@googlegroups.com.
To unsubscribe from this group, send email to angular+u...@googlegroups.com.
Visit this group at http://groups.google.com/group/angular?hl=en-US.
 
 

Noe Nieto

unread,
Dec 6, 2012, 3:05:26 PM12/6/12
to ang...@googlegroups.com
Thanks everybody.

I just made some synchronization by manipulating a flag inside my controller and then, when the flag is true the jquery plugin is executed. The flag becomes true after the data has finished loading.

But i like the idea of emiting my own signal when the data is ready.
Also I'll read more about ng-view, since i haven't used it too much (besides reading about it in the tutorial).

Again, thanks for the input.

---
Noe
--
Reply all
Reply to author
Forward
0 new messages