In AngularJS, I see sometimes we use $state.transitionTo()
and sometimes we use $state.go()
. Can anyone tell me how they differ and when one should be used over the other?
The $state.go is an AngularJS directive that tells the view to update its URL to reflect the current state of the application. This directive is used to change the URL without navigating away from the current page.
$stateParams is a service that captures URL-based parameters, and we can use these parameters to display information according to the state. Let's create an example with two states, understand how states work, and use $stateparams to store parameters. We will use $stateProvider to create a state.
Are you referring to the AngularUI Router? If so, the wiki specifies the differences:
$state.go(to [, toParams] [, options])
Returns a Promise representing the state of the transition.
Convenience method for transitioning to a new state.
$state.go
calls$state.transitionTo
internally but automatically sets options to{ location: true, inherit: true, relative: $state.$current, notify: true }
. This allows you to easily use an absolute or relative to path and specify only the parameters you'd like to update (while letting unspecified parameters inherit from the current state).
$state.transitionTo(to, toParams [, options])
Returns a Promise representing the state of the transition.
Low-level method for transitioning to a new state.
$state.go()
usestransitionTo
internally.$state.go()
is recommended in most situations.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With