angular icon indicating copy to clipboard operation
angular copied to clipboard

Route change doesn't trigger Location.subscribe

Open arpit-agarwal opened this issue 10 years ago • 5 comments

When changing routes using new router with angular 2.0.0.rc1, Location.subscribe never gets triggered.

It should trigger the subscription, since it's the only method available to check for URL changes in new router.

Steps to reproduce and a minimal demo of the problem http://plnkr.co/edit/hvtCJa?p=preview

What steps should we try in your demo to see the problem?

Click on Home/ List screen should poplate the text next to "Debug:"

Current behavior location.susbcribe not being called on route change

Expected/desired behavior location.susbcribe should be called on route change

Other information similar issue : https://github.com/angular/angular/issues/8630

arpit-agarwal avatar May 19 '16 10:05 arpit-agarwal

Location.subscribe only emits on window.popstate aka history navigation.

You can subscribe on router.changes

constructor(router: Router, location: Location) { 
  router.changes.subscribe(() => console.log(location.path())) 
}

Sadly the router pushStates the url immediately after history.back and prevents more then one history navigation attempt (https://github.com/angular/angular/blob/master/modules/@angular/router/src/router.ts#L151). Don't know the reasoning behind that.

sod avatar May 19 '16 21:05 sod

@sod Yes I tested this is getting called on history navigation. But shouldn't this be called for when route changes as well.

arpit-agarwal avatar May 20 '16 04:05 arpit-agarwal

Are there any news from the google team about this? It looks like it is affecting some projects related to analytics that detects this change. How we can help?

RicardoD7I avatar Sep 10 '16 13:09 RicardoD7I

This has been raised now deprecated router. The latest one @angular/router version 3.0.0.rc1 has no such limitation.
You can subscribe to' router.events'. [See ] [1] [1] https://angular.io/docs/ts/latest/api/router/index/Router-class.html#!#events-anchor

arpit-agarwal avatar Sep 12 '16 03:09 arpit-agarwal

This issue has been automatically locked due to inactivity. Please file a new issue if you are encountering a similar or related problem.

Read more about our automatic conversation locking policy.

This action has been performed automatically by a bot.