在AngularJS中,我看到有时我们使用$state.transitionTo()
,有时我们使用$state.go()
。谁能告诉我它们有什么不同,什么时候应该用在另一个上?
发布于 2014-01-14 11:53:48
你指的是AngularUI Router吗?如果是这样的话,维基specifies the differences
$state.go(to,toParams)
返回表示转换状态的Promise。
转换到新状态的便捷方法。$state.go
在内部调用$state.transitionTo
,但会自动将选项设置为{ location: true, inherit: true, relative: $state.$current, notify: true }
。这使您可以轻松地使用绝对或相对路径,并仅指定要更新的参数(同时让未指定的参数从当前状态继承)。
$state.transitionTo(to,toParams,options)
返回表示转换状态的Promise。
转换到新状态的低级方法。$state.go()
在内部使用transitionTo
。在大多数情况下,建议使用$state.go()
。
发布于 2017-04-07 17:58:00
$state.transitionTo
转换到一个新的状态。在大多数情况下,你不必使用它,你可能更喜欢$state.go
.
它接受options
对象中的一些参数:
如果true
将更新地址栏中的url,则返回
location
:;如果false
不更新,则返回url。If string record.inherit
:,将更新url,如果url.relative
将从当前from.notify
:继承events.reload
:参数,还将替换上一次历史"replace"
( true
将继承当前url参数(stateObject,null
__)):当使用相对路径(例如,'^')转换时,定义哪个状态将是相对from.notify
:如果为true
,将广播$stateChangeStart
和$stateChangeSuccess
url如果true
将强制转换,即使状态或参数没有更改,又名重新加载相同的状态。$state.go
是一种快捷方式,它使用默认选项调用$state.transitionTo
:
location
:true
inherit
:true
relative
:$state.$current
notify
:true
reload
:false
由于synthax更简单,所以更方便。您只能使用州名称来调用它。
$state.go('home');
https://stackoverflow.com/questions/21105528
复制相似问题