在AngularJS UI的路由器的UI SREF和$ state.go的区别 [英] Difference between ui-sref and $state.go in AngularJS UI-Router
问题描述
有没有之间的功能差异 UI-SREF
和 $ state.go()
?
Is there any functional difference between ui-sref
and $state.go()
?
UI-SREF
在中所使用; A> ...< / A>
和 $ state.go('someState')
是在控制器中使用。
ui-sref
is used in <a>...</a>
and $state.go('someState')
is used in a controller.
在HTML,我会用:
<a ui-sref="currentState.state1">Link</a>
而在功能我会用这样的:
whereas in a function I would use something like:
if(someCondition) {
$state.go('currentState.state1');
}
那么,是它还是我需要在 $ state.go补充一下()
?假设当前状态 currentState的
。
推荐答案
有没有 功能的之间的差异 UI-SREF
和 $ state.go
。看到该文档
There is no functional difference between ui-sref
and $state.go
. See the doc
有激活状态的方式主要有三种:
There are three main ways to activate a state:
- 呼叫
$ state.go()
。高层次的便捷方法。 - 单击包含
链接UI的SREF
指令。 - 导航到的
URL与状态有关。
- Call
$state.go()
. High-level convenience method. - Click a link containing the
ui-sref
directive. - Navigate to the
url
associated with the state.
那么,这些都是在最后做同样的,当我们在的<一的code看到href=\"https://github.com/angular-ui/ui-router/blob/b973dad155ad09a7975e1476bd096f7b2c758eeb/src/stateDirectives.js#128\"><$c$c>ui-sref$c$c>指令:
So, these are at the end doing the same, and as we can see in the code of the ui-sref
directive:
...
element.bind("click", function(e) {
var button = e.which || e.button;
if ( !(button > 1 || e.ctrlKey || e.metaKey || e.shiftKey || element.attr('target')) ) {
var transition = $timeout(function() {
// HERE we call $state.go inside of ui-sref
$state.go(ref.state, params, options);
});
它调用的 $ state.go()
it does call $state.go()
的同时也可作为这里讨论: UI-SREF :的
的 UI-SREF ='Statename的
- 导航到状态,没有PARAMS。 Statename的'可以是任何有效的绝对或相对的状态,遵循相同的语法规则 $ state.go()
的
ui-sref='stateName'
- Navigate to state, no params. 'stateName' can be any valid absolute or relative state, following the same syntax rules as$state.go()
这篇关于在AngularJS UI的路由器的UI SREF和$ state.go的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!