Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

location is not updated when url is an empty string #3563

Closed
lafeuil opened this issue Nov 21, 2017 · 3 comments
Closed

location is not updated when url is an empty string #3563

lafeuil opened this issue Nov 21, 2017 · 3 comments
Labels

Comments

@lafeuil
Copy link

lafeuil commented Nov 21, 2017

When I configure a state with this params :

state = {
  name: 'main',
  url: '/{id}',
  params: {
    id: {
      squash: true,
      value: null
    }
  },
}

and I go to this state with $state.go('main'), the location address bar is not updated.
I take a look at code and I find that the new path is correctly generated by the urlMatcher and it returns an empty string. So, the new url in location address bar must be for example http://localhost.
But the location service doesn't update the location when url is an empty string. The condition stops updating :

if (newUrl) this.$location.url(newUrl);

if (newUrl) this.$location.url(newUrl);

@lafeuil
Copy link
Author

lafeuil commented Nov 28, 2017

I have found a workaround to reinitialize the url in location address bar.

I define the onEnter and onRetain hooks in the state to fix the url to an empty string when the param id is not defined :

// FIX https://github.com/angular-ui/ui-router/issues/3563
onEnter: ['$location', '$transition$', function($location, $transition$) {
    var targetState = $transition$.targetState()
    if (targetState.name() === 'main.show' && !targetState.params().id) $location.url('')
}],
onRetain: ['$location', '$transition$', function($location, $transition$) {
    var targetState = $transition$.targetState()
    if (targetState.name() === 'main.show' && !targetState.params().id) $location.url('')
}]

@christopherthielen
Copy link
Contributor

@aj-dev
Copy link
Contributor

aj-dev commented May 4, 2018

I have experienced a weird side effect caused by this bug fix. All state config tests started failing and the reason was that I had 'url': '' in state configs

$stateProvider.state('anon.landing', {
            url: '',
            abstract: true,
            views: {
               ...
                }
            }
        });

so this.$location.url() in https://github.com/angular-ui/ui-router/blob/1.0.16/src/locationServices.ts#L71 was being invoked with an empty string. Removing 'url': '' option from state config solved it. Then newUrl is undefined and if (isDefined(newUrl)) this.$location.url(newUrl); does not update location url.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants