$routeChangeSuccess not fired if no ng-view element?

3,394 views
Skip to first unread message

Siong-Ui Te

unread,
Mar 7, 2013, 3:27:48 PM3/7/13
to ang...@googlegroups.com
Hello,

I found that $routeChangeSuccess is not fired if no ng-view element. does this behavior work as designed or is it a bug? Could somebody explain to me? thanks.

Siong-Ui


Peter Bacon Darwin

unread,
Mar 8, 2013, 4:10:37 AM3/8/13
to ang...@googlegroups.com
Can you provide a demo?



Siong-Ui


--
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 angular+u...@googlegroups.com.
To post to this group, send email to ang...@googlegroups.com.
Visit this group at http://groups.google.com/group/angular?hl=en-US.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Siong-Ui Te

unread,
Mar 8, 2013, 7:07:22 AM3/8/13
to ang...@googlegroups.com
http://routechangesuccess.epalitipitaka.appspot.com/

Please view source code for how I did this. Tested under my Chrome 25.

the first two link gives http 404. The last two changes url successfully, but no $routeChangeSuccess event. If I add <div ng-view></div> in html, everything is ok. I think this is a bug. Please confirm! Thanks.

Siong-Ui Te

unread,
Mar 8, 2013, 7:19:57 AM3/8/13
to ang...@googlegroups.com
Update: after uploading the demo to google app engine, all links change url successfully but no $routeChangeSuccess event.

Peter Bacon Darwin

unread,
Mar 8, 2013, 11:56:38 AM3/8/13
to ang...@googlegroups.com
I suspect that the $route service is not even being instantiated because you are not referencing it in your application. Using ng-view instantiates it because it depends upon $route.
Try adding $route as a dependency to your app run block.
(By the way, your route controllers are not created by the $route service - they are created by ng-view so you would need ng-view for those to be instantiated and run.)
Pete

Siong-Ui Te

unread,
Mar 8, 2013, 12:06:39 PM3/8/13
to ang...@googlegroups.com
Yes, you are right. if $route is added to the run block, everything works fine. From user point of view, I personally think this is not correct behavior. Also in the documentation this is no mention about this. Should I open a bug on GitHub?

Peter Bacon Darwin

unread,
Mar 8, 2013, 5:29:55 PM3/8/13
to ang...@googlegroups.com
This is the correct behaviour.  Services are only instantiated (lazily) when they are needed.  This actually gives performance benefits and prevents unnecessary code from being executed if not needed.

Siong-Ui Te

unread,
Mar 8, 2013, 5:41:23 PM3/8/13
to ang...@googlegroups.com
Thanks for the explanation.
If a user uses $routeProvider in the config block, then he definitely needs $route. i.e., if a user uses $fooProvider, then he definitely needs $foo.
Does this sound reasonable for you? If not, why is it not reasonable?

Siong-Ui Te

unread,
Mar 8, 2013, 5:49:59 PM3/8/13
to ang...@googlegroups.com
Another question is: in the demo $routeProvider is configured and the route does change, but no $routeChangeSuccess event. Is it also a correct behavior?

Peter Bacon Darwin

unread,
Mar 8, 2013, 5:51:50 PM3/8/13
to ang...@googlegroups.com
Not sure what you mean there?  How does the route change?  Do you mean the URL changes?

Siong-Ui Te

unread,
Mar 8, 2013, 5:56:10 PM3/8/13
to ang...@googlegroups.com
Yes, they are not the same? I suppose they are the same...

Peter Bacon Darwin

unread,
Mar 8, 2013, 5:58:19 PM3/8/13
to ang...@googlegroups.com
You can change URLs (with $location for instance), but not necessarily update a route (i.e. $route and $routeParams).

Siong-Ui Te

unread,
Mar 8, 2013, 6:12:33 PM3/8/13
to ang...@googlegroups.com
This is so strange, in the demo:
$routeProvider.when('/', {template: '<br />', controller: noopCtrl});
$routeProvider.when('/en_US/', {template: '<br />', controller: noopCtrl});
$routeProvider.when('/zh_TW/', {template: '<br />', controller: noopCtrl});
$routeProvider.otherwise({redirectTo: '/'});



The routes are already defined, and URLs are also changed, why are routes not updated?

Peter Bacon Darwin

unread,
Mar 8, 2013, 6:19:10 PM3/8/13
to ang...@googlegroups.com
Because you haven't referenced $route so the route matching engine has not been instantiated and it is not watching the $location changes?

Siong-Ui Te

unread,
Mar 8, 2013, 6:23:55 PM3/8/13
to ang...@googlegroups.com
In the official documentation of $route:
---

Is used for deep-linking URLs to controllers and views (HTML partials). It watches $location.url() and tries to map the path to an existing route definition.

You can define routes through $routeProvider's API.

---
From the above description, do you think the $route should be updated even if it is not referenced?

Mike Fabrikant

unread,
Jul 26, 2013, 6:07:41 PM7/26/13
to ang...@googlegroups.com

I created this plunker to demonstrate Peter Darwin's advice for initiating $route service by adding $route as a dependency in the app run block as an alternative to adding ng-view.
Reply all
Reply to author
Forward
0 new messages