AngularJS UI-Router 中 ui-sref 和 $state.go 的区别 [英] Difference between ui-sref and $state.go in AngularJS UI-Router

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

问题描述

ui-sref$state.go() 在功能上有区别吗?

ui-sref 用于 ...$state.go('someState') 用于控制器.

在 HTML 中,我会使用:

链接

而在函数中我会使用类似的东西:

if(someCondition) {$state.go('currentState.state1');}

那么,是不是我需要在 $state.go() 之后添加一些东西?假设当前状态是 currentState.

解决方案

ui-sreffunctional 区别>$state.go.查看文档

激活状态

<块引用>

激活状态主要有以下三种方式:

  • 调用$state.go().高级便利方法.
  • 单击包含 ui-sref 指令的链接.
  • 导航到与状态关联的 url.

所以,这些在最后做同样的事情,正如我们在 ui-sref 指令:

<预><代码>...element.bind("点击", function(e) {var button = e.which ||e.按钮;if ( !(button > 1 || e.ctrlKey || e.metaKey || e.shiftKey || element.attr('target')) ) {var transition = $timeout(function() {//这里我们在 ui-sref 中调用 $state.go$state.go(ref.state, params, options);});

它确实调用了 $state.go()

还有这里讨论的:ui-sref:

<块引用>

ui-sref='stateName' - 导航到状态,无参数.'stateName' 可以是任何有效的绝对或相对状态,遵循与 $state.go()

相同的语法规则

Is there any functional difference between ui-sref and $state.go()?

ui-sref is used in <a>...</a> and $state.go('someState') is used in a controller.

In HTML, I would use:

<a ui-sref="currentState.state1">Link</a>

whereas in a function I would use something like:

if(someCondition) {
    $state.go('currentState.state1');
}

So, is that it or do I need to add something after $state.go()? Assuming current state is currentState.

解决方案

There is no functional difference between ui-sref and $state.go. See the doc

Activating a state

There are three main ways to activate a state:

  • Call $state.go(). High-level convenience method.
  • Click a link containing the ui-sref directive.
  • Navigate to the url associated with the state.

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);
      });

it does call $state.go()

And also as discussed here: ui-sref:

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-Router 中 ui-sref 和 $state.go 的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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