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?
2 Answers
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.gocalls$state.transitionTointernally 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()usestransitionTointernally.$state.go()is recommended in most situations.
- 1
- 1
- 157,729
- 40
- 374
- 311
-
1I found more information I need from your given links. Thank so much Brandon :) – Barcelona Jan 14 '14 at 04:15
-
1Apparently, the values inputted in the view do not flush off if I re-trigger the view using transitionTo. Anyway we can force refresh the values/js/view? P.S. - Page reload is not an option as the view is an overlay – Swanidhi Mar 16 '15 at 07:06
$state.transitionTo transite to a new state. In most cases, you don't have to use it, you may prefer $state.go.
It takes some parameters in an options object:
location: Iftruewill update the url in the location bar, iffalsewill not. If string"replace", will update url and also replace last history record.inherit: Iftruewill inherit url parameters from current url.relative(stateObject, defaultnull): When transitioning with relative path (e.g '^'), defines which state to be relative from.notify: Iftrue, will broadcast$stateChangeStartand$stateChangeSuccessevents.reload: Iftruewill force transition even if the state or params have not changed, aka a reload of the same state.
$state.go is a sort of shortcut that call $state.transitionTo with default options:
location:trueinherit:truerelative:$state.$currentnotify:truereload:false
It is more convenient as the synthax is simpler. You can call it only with a state name.
$state.go('home');
- 17,793
- 13
- 73
- 97