ui-router icon indicating copy to clipboard operation
ui-router copied to clipboard

state transition performed twice when using optional state parameters

Open 3plusalpha opened this issue 12 years ago • 24 comments

When optional state parameters are defined for a state, the state transition is performed twice as long as the optional parameters.

The $stateParams of a controller that was called twice look like this: First: {offset: undefined, limit: undefined, sort: undefined, sortby: undefined} Second: {offset: 0, limit: 20, sort: "asc", sortby: "title"}

Note that all state parameters are optional and defined like this.

.state({
  name: "list.products",
  parent: "list",
  url: "/products/:offset/:limit/:sortby/:sort/",
  params: {
    offset: { value: 0 },
    limit: { value: 20 },
    sort: { value: 'asc' },
    sortby: { value: 'title' }
  },
  templateUrl: "views/partials/product-search-result-item.html",
  controller: "ListProductsController"
})

When I use ui-sref without any parameter the controller is called twice. Products

Using all state parameters the controller is called only once. Products

IMHO this is a bug and I hope somebody can fix it as all controllers with optional parameters and API calls make API requests twice.

3plusalpha avatar Sep 23 '14 21:09 3plusalpha

I think I've seen this as well. Will you please make a plunk?

christopherthielen avatar Sep 23 '14 22:09 christopherthielen

Here is the plunkr: http://plnkr.co/edit/uQEznMA2fadSYQg3fCvc

As soon as I added the ID parameter to the URI (route1/:id) the controller is called twice

3plusalpha avatar Sep 24 '14 20:09 3plusalpha

Here's what's happening:

  1. ui-sref calls transitionTo
  2. transitionTo was not applying default params when building toParams.
  3. transitionTo completes and saves the partial toParams as fromParams
  4. after the transition, the URL is sync'd.
  5. the sync code uses UrlMatcher which applies default params, and calls transitionTo
  6. normally, transitionTo exits early since the state and params are identical. However, in this case, the now fromParams (step 3's toParams) don't have defaults, while step 4's params have them, so the transition occurrs again.

patched plunk with fix from #1424 http://plnkr.co/edit/Wiej3lypbhPKFkAsRNoZ?p=preview

christopherthielen avatar Oct 04 '14 02:10 christopherthielen

Thank you Chris for digging into that issue!

3plusalpha avatar Oct 04 '14 10:10 3plusalpha

hi, i'm also experiencing this using routeProvider, '/page/:id?' has this fix also addressed routeProvider as well?

uptownhr avatar Nov 02 '14 06:11 uptownhr

Hi, I'm seeing this when using version 0.2.13. Instead of using ui-sref I have am using ng-click to call a function which then calls $state.go(). I can also confirm this when using $state.transitionTo().

ng-click="selectAlert(alert)

var selectAlert = function(alert){ $state.go('app.alerts.list.detail', {alertId: alert.id}); }

Does this bug still exist or am I using this incorrectly?

nolandubeau avatar Feb 19 '15 03:02 nolandubeau

I had same problem like @nolandubeau . version is 0.2.11. call $state.go in ng-click. some $state parameters are null.

Seems to be fixed in 0.2.14.

realdah avatar May 02 '15 02:05 realdah

i am still getting the same problem in 0.2.15 ,is the problem fixed or still persist ,please help me with this one

pranaydutta89 avatar Jul 29 '15 06:07 pranaydutta89

I also am getting this problem in 0.2.15. I am using custom types in my solution.

bryanerayner avatar Aug 20 '15 14:08 bryanerayner

Please make a plunker

christopherthielen avatar Aug 21 '15 11:08 christopherthielen

+1, have the same problem in 0.2.15.

Gratouille avatar Aug 31 '15 09:08 Gratouille

its working fine with 0.2.14,

pranaydutta89 avatar Aug 31 '15 10:08 pranaydutta89

Ok, nevermind, it was a problem of implementation of my login router. In fact I did it in my $stateChangeStart and it didn't end well. So I think I have to move this out in another listener.

Gratouille avatar Aug 31 '15 10:08 Gratouille

I agree with @pranaydutta89; for me using version 0.2.14 instead if 0.2.15 fixes my issue. There does seem to be another issue here, which I am looking into.

blowsie avatar Sep 16 '15 14:09 blowsie

@christopherthielen I have a Plunkr which demonstrates what I'm trying to do to make some sort of dynamic parameters work in 0.2.15. http://plnkr.co/edit/Yeex1Rmd1nWQvdrxj87N?p=preview. It works to change parameters syncronously, but not via a $timeout.

bryanerayner avatar Sep 18 '15 17:09 bryanerayner

Im getting this problem in 0.2.15.

Using 0.2.14. fixed the problem to me.

levivm avatar Oct 08 '15 05:10 levivm

+1 Same for me, I had to go back to 0.2.14 to fix the problem

m2omou avatar Oct 19 '15 17:10 m2omou

+1 Same here

petr0001 avatar Oct 20 '15 10:10 petr0001

Actually it's also happening in the version 0.2.14 even though this version looks more stable than the 0.2.15.

m2omou avatar Oct 20 '15 15:10 m2omou

Can confirm this for version 0.2.15. When using nested views and a parameter is present in ui-sref, then the parent resolve function is fired for a second time. Removing the parameter makes it work as expected.

zszep avatar Oct 22 '15 15:10 zszep