【问题标题】:Navigating to a child state refreshes its parent state too导航到子状态也会刷新其父状态
【发布时间】:2016-08-10 00:52:54
【问题描述】:

我将尝试提供一个示例伪代码,以便能够尽可能详细地解释我的问题。我无法在此处放置工作代码,因为其中涉及太多层、指令和模板。

我的基本结构是:

...
<div>
   <div ui-view>
       <div ui-view></div>
   </div>
</div>
...

当父 ui-view 中的代码执行 $state.go 以更新子 ui-view 时,父代码也会再次加载。

假设父状态称为contact,子状态称为details。我的$state.go 调用是$state.go("contact.details")$stateProvider 配置如下所示:

$stateProvider.state({ name: "contact" /*,  rest of properties */ });
$stateProvider.state({ name: "contact.details", /*,  rest of properties */ });

此外,整个子 ui-view 发生在指令嵌入中。假设它是:

<div>
   <div ui-view>
       <directive>
           <content>
                <div ui-view></div>
           </content>
       </directive>
   </div>
</div>

通过这些详细信息,您是否发现任何问题?为什么$state.go("contact.details") 也重新加载contact? (请注意,contact.details 已加载,但重新加载 contact 有副作用,因为控制器再次执行...)。

更新

如果我在contact 的控制器上调用$state.go("details"),它会发生同样的问题,但是一旦加载,当我$state.go 到其他子状态时,每个子状态都会按预期打开,但是再次调用父状态的控制器

【问题讨论】:

  • 你为什么使用两个未命名的uiView指令??
  • @Hitmands 好吧,它们是无人驾驶的,但属于不同的州
  • @Hitmands 看到我说的第二个子ui-view是一些指令嵌入的内容

标签: javascript angularjs html angularjs-directive angular-ui-router


【解决方案1】:

最后,我想出了如何解决我的问题/场景。

  1. 我已切换到使用 dot 语法 作为第一个参数来注册子状态。例如$stateProvider.state("a.b", {}) 而不是$stateProvider.state({ name: "b", parent: "a" });。我知道我的问题是我使用name: "a.b" 注册状态。自发布此问答以来,我已多次更改代码。

  2. 我现在在子状态的 controller 中调用 $state.go("a.b") 以打开默认子状态(这是问题的根源)。请参阅Angular JS ui-router how to redirect to a child state from a parent?

【讨论】:

    【解决方案2】:

    我没有问题,但我和你有不同,我的父状态要么是抽象的,要么重定向到其控制器中的默认子状态。

    所以我有

    $stateProvider.state({ name: "contact" /*,  rest of properties */ 
          controller:['$state', function($state){
             if($state.current.name=='contact'){
                  $state.go('contact.main');
             }
          }]
     });
    $stateProvider.state({ name: "contact.main", /*,  rest of properties */ 
    $stateProvider.state({ name: "contact.details", /*,  rest of properties */ });
    

    【讨论】:

    • 我明天都会检查这个。顺便说一句,这就是我已经做的(请参阅我的更新更新问题)
    • 虽然到目前为止它没有提供任何价值,但我已经在解决我的问题方面取得了进展。顺便说一句,解决我的问题的最后一个前沿是,当我进入子状态时,每当我更改为子状态时,都会再次调用相当于 contact 的控制器。
    猜你喜欢
    • 2015-07-03
    • 1970-01-01
    • 1970-01-01
    • 2023-01-18
    • 1970-01-01
    • 1970-01-01
    • 2017-10-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多