Jarek
Jarek

Reputation: 7729

Angular2 - changing location without triggering a Route

I have a view which uses service to load a list of items. I'm always displaying only one item, moving between items with << >> buttons.

Is there any way to change address in browser's address bar and to add record to browser's history without triggering Angular's routing system?

I know that if I use:

location.go('users/3');

it will do above-mentioned things, but it also triggers Angular's route system, which in turn calls my services again.

Edit:

it seems like location's function

location.replaceState('users/3');

is getting close to what I want to do - it changes url in browser without triggering any Angular internals, unfortunately it replaces latest location in history instead of pushing the new one at the top.

Upvotes: 4

Views: 4917

Answers (1)

G&#252;nter Z&#246;chbauer
G&#252;nter Z&#246;chbauer

Reputation: 658255

If you don't add the service to the components provider: list you won't get a new instance for this service. Make the service global instead by only registering in bootstrap(AppComponent, [MyGlobalService]).

Implementing CanReuse see also Angular 2: Swapping between different components without destroying them might help to prevent re-initializing components when the same route is reused.

Upvotes: 1

Related Questions