zoukankan      html  css  js  c++  java
  • 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设计的让人郁闷。

  • 相关阅读:
    20162329张旭升 2017-2018-2 《程序设计与数据结构》第一周学习总结
    20162329 张旭升2016-2017《程序设计与数据结构》课程总结
    实验报告五
    20162329 张旭升 阶段四则运算(挑战出题)
    实验四:Android 开发基础
    四则运算的整体总结(第二周)
    结对编程四则运算(阶段总结)
    团队项目-选题报告
    第二次结对编程作业
    第一次结对编程作业
  • 原文地址:https://www.cnblogs.com/binlin1987/p/7249728.html
Copyright © 2011-2022 走看看