angular-ui / ui-router

The de-facto solution to flexible routing with nested views in AngularJS
http://ui-router.github.io/
MIT License
13.53k stars 3k forks source link

Changing URL without changing state #64

Closed iliakan closed 10 years ago

iliakan commented 11 years ago

Here's the use case to prove my point.

I'm editing a Model. On save I want to change URL to /model/:id (id comes form server), but calling $state.transitionTo('model', {id: ...}) causes the state change and hence the model is RELOADED. As if it were not on client!

I'm trying to pass the existing model to the state, so that may keep it (by reusing in resolve): state.transitionTo('model', {model: modelObject}). But the normalization code stringifies the "model", so that's not an object any more.

Is what I'm doing conceptually wrong? Is there any really good reason to keep normalization which prevents it from working?

alexandrejunges commented 8 years ago

I've tried almost all solutions suggested here, but couldn't make the url change work properly. When I call $state.go it works as expected, not re-initiating the controller, however when I move to a new state it re-initiate the old controller.

My route is something like this: host/parent/{id}/child, and I'm changing the state like this:

this.$state.go(this.$state.current.name, param, {
   notify: false,
   reload: false,
   location: 'replace'
});

Has anyone found a solution for this?

Mikaeru001 commented 8 years ago

@alexandrejunges Worked for me on 0.2.18

$state.go($state.$current.self.name, stateParams, {notify: false});
averas commented 8 years ago

@Mikaeru001 By "worked", do you mean that the transitions happen as expected, or do you mean that you manage to execute the transitions without the controller reloading when executing subsequent transitions? I think what most people is having trouble with is the latter. I've also managed to get the transitions to work precisely as I want, but my controllers keep reloading...

Mikaeru001 commented 8 years ago

@averas I mean the method works as expected. URL changes and the state stays the same. Controller is not initialized twice. On 0.2.13 I had another problem with {notify: false}. On $state.go call URL changes, state isn't reloaded. But when I click a link to go to another state, the state from the previous $state.go({notify: false}) appears. It is not a case now since I'm on 0.2.18.

Mikaeru001 commented 8 years ago

@averas Or is it exactly the same problem you described?

averas commented 8 years ago

This is what I experience.

Say that I am on state: x.y.z

I execute $state.go(a.b.c, stateParams, {notify: false, location: "replace"});

This works, the URL is replaced, nothing appears to reload or anything.

I then want to get back to my previous state, so I do a:

$state.go(x.y.z, stateParams, {notify: false, location: "replace"});

This also seem to work, the URL is changed, everything looks good. However, when I then do a normal transition to a sibling state, which normally only loads the last controller in the chain:

$state.go(x.y.a)

.. all controllers (including the ones for x and y) reload. This does only happen after I changed the URL like above. I think what most people are saying in this thread is that you do not need to go back and forth like I do for this to happen. When you start interacting after you have performed the URL-replacing trick the controller reloads.

Mikaeru001 commented 8 years ago

@averas Sorry. Can't help you with that.

alexandrejunges commented 8 years ago

Hi @Mikaeru001, I was using 0.2.15 and updated to 0.2.18, but the controller keeps being reloaded after I change the URL, as described by @averas.

Did you change another thing?

averas commented 8 years ago

@Mikaeru001 I honestly believe that you will strike the same problem as soon as you start interacting in your own application, you just don't know it yet. ;-)

Mikaeru001 commented 8 years ago

@averas Probably. @alexandrejunges No. Just what I've copy-pasted previously.

orangesoup commented 8 years ago

Any update on this one? I'm trying to do exactly what @averas said with 1.0, but so far no luck...

MaximShoustin commented 8 years ago

credits to @cseils if ($state.transition) return; works for me so far. For sure this hack is workaround so wait with you aligant solution.

**I added if ($state.transition) return; to all controllers that change URL without reloading

guikubivan commented 8 years ago

In case anybody is wondering, this is working for me now: https://github.com/angular-ui/ui-router/issues/1758#issuecomment-205060258. To be specific, just adding {dynamic: true} to a param allows me to use $state.go like normal and it does not reload the controller.

Moouren commented 6 years ago

Hi Everyone. i have a situation that i have a community type page that load recent posts lazy load on scroll and on post click , open a modal and change url , but when close modal go back to previous state and do not reload page