在AngularJS UI的路由器的UI SREF和$ state.go的区别 [英] Difference between ui-sref and $state.go in AngularJS UI-Router

查看:347
本文介绍了在AngularJS UI的路由器的UI SREF和$ state.go的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有之间的功能差异 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指令:

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屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆