4

There doesn't appear to be a built-in way to update the url without running all of the other processes, e.g. invoking controllers.

The option "reload", only works with $location.search()

Is there a method, or approach to achieve

How may I achieve something similar to:

$state.go('stateName', params, {justChangeTheUrl: true})

With Angular UI-Router?

1
  • just define your state url with this param {url: 'stateName?reload}' Commented Aug 4, 2014 at 20:33

2 Answers 2

3

If you just want to change the url without having that change processed by ui-router then you can use the following perfectly acceptable hack (coffeescript):

scopeOff = $scope.$on '$stateChangeStart', (e) ->
  e.preventDefault()
  scopeOff()

$location.path 'just/change/the/url'

Notes

Normally making a change to the browser url via $location.path() would trigger ui-router to begin its processing. This processing starts with the ui-router specific event called $stateChangeStart.

In the code above, however, you are catching and, via e.preventDefault(), throwing away this initial call to $stateChangeStart event and so preventing ui-router from processing the url change.

The watch on the $stateChangeStart event is 'single use', that is the code automatically detaches itself from the event after one run (this is what the call to scopeOff() is doing).

The net effect is to change the url in the browser without triggering ui-router processing and to only apply this restriction to the one url you have just changed. Neat, but not as neat as the new deferIntercept feature that will be with us soon.

Sign up to request clarification or add additional context in comments.

3 Comments

Won't preventDefault prevent the url from changing too?
@TaylorMac Please see the Notes I have added to the answer.
Note on deferIntercept, this allows you to intercept the stateChange process, but it appears that you can only modify when the entire process (of the route updating and state changing) occurs. So to update the URL without changing the state, you would need to not just Defer the process, but cancel it completely, and update the url manually yourself, which is completely prevented by Angular's $route service.
0

Define state parameter in url

 $stateProvider.state('stateName', {
     url: '/state/path?reload'
 });

But be carefull, every $location.search() change on reload param will trigger $stateChangeStart event, and your controller will be recreated

2 Comments

What about if we don't want the state to reload only sometimes, I think this is why having an option at the call site is better
yes it is, but there are no proper solution, just wierd hacks

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.