简体   繁体   English

Angular $state.go {reload: true} 不应该重新初始化抽象父控制器

[英]Angular $state.go {reload: true} shouldn't reinitialize the abstract parent controller

I'm using ui-route for navigation.我正在使用 ui-route 进行导航。
I have father state called main , it's an abstract state (the url: /main ) and child states products and users (urls: /main/products and /main/users ).我有一个名为main 的父状态,它是一个abstract状态(网址: /main )和子状态产品用户(网址: /main/products/main/users )。

app.config(["$stateProvider", "$urlRouterProvider",
  function($stateProvider, $urlRouterProvider) {
    $urlRouterProvider.otherwise("/main/products");
    $stateProvider
      .state("main", {
        url:"/main",
        templateUrl: "main.html",
        abstract: true,
        controller: "MainCtrl",
      })
      .state("main.products", {
        templateUrl: "products.html",
        controller: "productsCtrl",
      })
      .state("main.users", {
        templateUrl: "users.html",
        controller: "usersCtrl",
      })
  }
]);

And here my controllers:这里是我的控制器:

app.controller('MainCtrl', function($scope,$state) {
  console.log("I'm Main controller")

  $scope.goToProduct = function()
  {
    //$state.go("main.products",{})
    $state.go("main.products",{},{reload:true})
  }

  $scope.goToUsers = function()
  {
    //$state.go("main.users",{})
    $state.go("main.users",{},{reload:true})
  }

});

app.controller('usersCtrl', function() {
  console.log("I'm users controller")

});

app.controller('productsCtrl', function() {
  console.log("I'm products controller")
});

The HTML: HTML:

<ul>
  <li style="cursor:pointer"  ng-click="goToProduct()">Click for products</li>
  <li style="cursor:pointer" ng-click="goToUsers()">Click for users</li>
</ul>

<br>
<div style="font-size:28px" ui-view></div>

As you can see, I'm using: $state.go("main.products",{},{reload:true}) for navigation如您所见,我使用: $state.go("main.products",{},{reload:true})进行导航

When I'm clicking on users/products on the menu the MainCtrl reinitialize !!当我点击菜单上的用户/产品时, MainCtrl重新初始化!!
It's because the {reload:true} .这是因为{reload:true}

My questions:我的问题:
1) Why the "father" state controller also reinitialize on each click? 1)为什么“父亲”状态控制器也会在每次点击时重新初始化?
2) I need an elegant solution to avoid the MainCtrl to reinitialize ! 2)我需要一个优雅的解决方案来避免MainCtrl重新初始化

Here is the complete example - plunker please look at the console.这是完整的示例 - plunker请查看控制台。

  1. Update your ui-router to newer version (0.2.14 at least)将您的 ui-router 更新到更新版本(至少 0.2.14)
  2. Change $state.go call to something like this $state.go("main.users",{},{reload: "main.users"})$state.go调用更改$state.go类似$state.go("main.users",{},{reload: "main.users"})

Since ui-router 0.2.14 you can pass string as reload value - ui router will refresh state that matches to passed string and all its children.从 ui-router 0.2.14 开始,您可以将字符串作为reload值传递 - ui 路由器将刷新与传递的字符串及其所有子项匹配的状态。

the reload: true option will reload current and parent states (basically from your current state to the highest level state). reload: true 选项将重新加载当前和父状态(基本上从当前状态到最高级别状态)。 However, if you specify reload option with state name.但是,如果您使用状态名称指定重新加载选项。 it only reload the state that you put in and its all children state.它只会重新加载您放入的状态及其所有子状态。 This is straight from the doc这是直接来自文档

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM