• Angularjs ui router,路由嵌套 父controller执行问题


    解决方式来源:https://stackoverflow.com/questions/25316591/angularjs-ui-router-state-reload-child-state-only/25322797#25322797

    路由路径设置:structured.text   ;structured是第一层路由,text是第二层路由;

    问题如下,当$state.transitionTo路由到第二层是,structured的controller也会执行一次,导致页面控件重新加载刷新。

    $state.transitionTo(
        "structured.text", 
        { args : $stateParams },
        { reload: false}
    );
    

    查了github看到 https://github.com/angular-ui/ui-router/issues/1612,说此问题已经被fixed,然而设置了reload: stateObj并没有卵用。

    I think this makes sense and is a good idea. I'd like to omit the second reloadState parameter, and overload the existing reload parameter.

    • { reload: true }// reloads all,
    • { reload: 'foo.bar' } // reloads top.bar, and any children
    • { reload: stateObj } // reloads state found in stateObj, and any children

    Squash your commits and set your commit message to match the contributor guidelines. If @nateabeleconcurs, I think we'll merge this in.

    直到看到了https://stackoverflow.com/questions/21105528/difference-between-state-transitionto-and-state-go-in-angular-ui-router

    $state.go(to [, toParams] [, options])

    Returns a Promise representing the state of the transition.

    Convenience method for transitioning to a new state. $state.go calls $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() uses transitionTo internally. $state.go() is recommended in most situations.

    $state.transitionTo是底层方法,用$state.go是用它实现的,$state.go设置了一些默认参数 ,于是用$state.go试了下,竟然成功;

    debug看了下原来是默认options inherit:true和reload:false共同引发的问题:

    错误写法1:

    $state.transitionTo(
        "structured.text", 
        { args : $stateParams },
        { reload: true}
    );    
    

     原因:路由写了两级,structured的controller会被重复执行

    错误写法2:

    $state.transitionTo(
          ".text", 
        { args : $stateParams },
        { reload: true}
    );   
    

     原因:relative没有设置和inherit默认又是false,报错路由找不到

    正确写法:

    $state.go(
        'structured.text',
       {},
       { reload: 'structured.text'}
    );

    https://github.com/angular-ui/ui-router/issues/1612#issuecomment-77757224 这里作为一个bug被fixed,路由设计还是太蠢...

    推荐使用$state.go, 用$state.transitionTo底层方法的话,options要都正确,问题解决,搞了半天研究这个破东西,api设计的让人郁闷。

  • 相关阅读:
    UE4_武器4_武器切换(下)
    UE4_武器3_武器切换(上)
    UE4_武器2_矛的攻击与动画
    UE4_武器1_创建武器
    Java知识体系复习笔记
    今日份学习: 毕设。。。。
    笔记:Spring security认证
    一个项目的记录:多人博客
    今日份学习: Spring
    今日份学习:部署?
  • 原文地址:https://www.cnblogs.com/binlin1987/p/7249728.html
Copyright © 2020-2023  润新知