why the $routeChangeSuccess event is triggered twice?

8,239 views
Skip to first unread message

Xi Shen

unread,
Dec 1, 2012, 7:27:03 AM12/1/12
to ang...@googlegroups.com
Hi,

Here's how I bind the event listener:

 $scope.$on('$routeChangeSuccess', function ($event, current) {
    console.log(current);
  });

In the console I see the event is triggered twice:

1st time, the $route object has the "redirectTo" property;
2nd time, the $route object has the "reloadOnSearch" property;

My questions are:

why the event is triggered twice?
why the $route object has different value?


Thanks,
David

Pawel Kozlowski

unread,
Dec 1, 2012, 7:28:53 AM12/1/12
to ang...@googlegroups.com
Could you share your routes definition? Ideally in a plunk.

I guess that you've got one route that redirects to another one
(.otherwise section?).

Cheers,
Pawel
> --
> 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.
>
>



--
Question? Send a fiddle
(http://jsfiddle.net/pkozlowski_opensource/Q2NpJ/) or a plunk
(http://plnkr.co/)
Need help with jsFiddle? Check this:
http://pkozlowskios.wordpress.com/2012/08/12/using-jsfiddle-with-angularjs/

Looking for UI widget library for AngularJS? Here you go:
http://angular-ui.github.com/

Xi Shen

unread,
Dec 1, 2012, 9:07:13 AM12/1/12
to ang...@googlegroups.com
Hi Pawel,

My route does not have the redirection section.

    $routeProvider.
      when('/blog').
      when('/blog/:year').
      when('/blog/:year/:month').
      when('/blog/:year/:month/:title/');

Please help :)

Pawel Kozlowski

unread,
Dec 1, 2012, 9:13:11 AM12/1/12
to ang...@googlegroups.com
Hi!

On Sat, Dec 1, 2012 at 3:07 PM, Xi Shen <david...@gmail.com> wrote:
>
> Please help :)

Please send a plunk :)

Cheers,
Pawel

Xi Shen

unread,
Dec 1, 2012, 9:17:45 AM12/1/12
to ang...@googlegroups.com
Sorry, I do not follow you :( Do you mean http://www.plunkapp.com/, this app? How do I use it?

Pawel Kozlowski

unread,
Dec 1, 2012, 9:50:53 AM12/1/12
to ang...@googlegroups.com
Hi!

On Sat, Dec 1, 2012 at 3:17 PM, Xi Shen <david...@gmail.com> wrote:
> Sorry, I do not follow you :( Do you mean http://www.plunkapp.com/, this
> app? How do I use it?

Sorry, I've meant this one: http://plnkr.co/

Cheers,
Pawel

Xi Shen

unread,
Dec 1, 2012, 10:07:18 AM12/1/12
to ang...@googlegroups.com
Hi,

Here it is: http://plnkr.co/edit/iw8xjlOCiFrr84EAaVzj. In the JS, you will see I am using "if (current && current.redirectTo !== undefined..." to avoid executing the code twice.


Thanks,
D.

Peter Bacon Darwin

unread,
Dec 1, 2012, 10:16:10 AM12/1/12
to ang...@googlegroups.com
It looks like you have a right mismatch of different libraries in there.  Can you extract out the AngularJS bit and demonstrate your problem?  I can't dig through all this but I suspect that the various points where you set window.forceLoad smells bad.


--

Xi Shen

unread,
Dec 1, 2012, 11:09:16 PM12/1/12
to ang...@googlegroups.com
Hi,

I updated the code in plunk. http://plnkr.co/edit/iw8xjlOCiFrr84EAaVzj, please take a look again. I can still repro the problem.


Thanks a lot!

Peter Bacon Darwin

unread,
Dec 2, 2012, 11:03:39 AM12/2/12
to ang...@googlegroups.com
Right now the console in Chrome says this

FB.getLoginStatus() called before calling FB.init(). connect.facebook.net/en_US/all.js:52
  1. Unsafe JavaScript attempt to access frame with URL http://plnkr.co/edit/iw8xjlOCiFrr84EAaVzj?p=preview from frame with URL http://static.ak.facebook.com/connect/xd_arbiter.php?version=17#channel=f3a3a9f0a4&origin=http%3A%2F%2Frun.plnkr.co&channel_path=%2FJieYCbepXWzmWvLk%2F%3Ffb_xd_fragment%23xd_sig%3Df2cf2327c%26. The frame requesting access set 'document.domain' to 'facebook.com', but the frame being accessed did not. Both must set 'document.domain' to the same value to allow access. xd_arbiter.php:20
  1. Unsafe JavaScript attempt to access frame with URL http://plnkr.co/edit/iw8xjlOCiFrr84EAaVzj?p=preview from frame with URL https://s-static.ak.facebook.com/connect/xd_arbiter.php?version=17#channel=f3a3a9f0a4&origin=http%3A%2F%2Frun.plnkr.co&channel_path=%2FJieYCbepXWzmWvLk%2F%3Ffb_xd_fragment%23xd_sig%3Df2cf2327c%26. The frame requesting access has a protocol of 'https', the frame being accessed has a protocol of 'http'. Protocols must match. xd_arbiter.php:20
Failed to load resource: the server responded with a status of 404 (Not Found) http://run.plnkr.co/blog/static/js/blog2.js
Uncaught Error: No module: blog angular.min.js:17


I would start by sorting all that out before asking for help on the routing.
Pete

Xi Shen

unread,
Dec 3, 2012, 1:06:16 AM12/3/12
to ang...@googlegroups.com
I am really sorry...I did not save the change :P



Thanks!

Lutz Epperlein

unread,
Dec 3, 2012, 4:31:32 AM12/3/12
to ang...@googlegroups.com
In your code I found a mismatch between the values of the href attrs and the path parameter of $outeProvider.when.

Your html:
<a href="/blog/2012/">2012</a>

Your route definition:
$routeProvider
.when('/blog')
.when('/blog/:year')

Change your href to:
<a href="/blog/2012">2012</a>
and it works ... or look here: http://plnkr.co/edit/6LhkErRRLwKwtvdP3KJq

HTH
Regards Lutz

Peter Bacon Darwin

unread,
Dec 3, 2012, 4:38:34 AM12/3/12
to ang...@googlegroups.com
You see! By cleaning up your example you get a sudden response.
Lutz is quite right, navigating to the url with the slash on the end results in a redirect to the version without the slash.
You can see this in this (even more cut-down example): http://plnkr.co/edit/pZfw9tMhFqtaVPV9WJaz?p=preview.
Pete

Xi Shen

unread,
Dec 3, 2012, 10:15:29 AM12/3/12
to ang...@googlegroups.com
Thank you guys! :)

Peter Williamson

unread,
Aug 26, 2013, 4:05:03 PM8/26/13
to ang...@googlegroups.com
Pawel you genius, I was being stupid and redirecting to "/home" instead of just "/" - triggering the double routeChangeSuccess.

Would have taken someone else a second to look at my code and notice that, being buried in your own code for too long is a problem.

Thanks Pawel and Xi for starting this thread!
Reply all
Reply to author
Forward
0 new messages