Angular $state.go {reload: true} 不应该重新初始化抽象父控制器 [英] Angular $state.go {reload: true} shouldn't reinitialize the abstract parent controller

查看:25
本文介绍了Angular $state.go {reload: true} 不应该重新初始化抽象父控制器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 ui-route 进行导航.
我有一个名为 ma​​in 的父状态,它是一个 abstract 状态(网址:/main)和子状态 products用户(网址:/main/products/main/users).

I'm using ui-route for navigation.
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).

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",
      })
  }
]);

这里是我的控制器:

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

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>

如您所见,我正在使用:$state.go("main.products",{},{reload:true})用于导航

As you can see, I'm using: $state.go("main.products",{},{reload:true}) for navigation

当我点击菜单上的用户/产品时,MainCtrl 重新初始化!!
这是因为 {reload:true}.

When I'm clicking on users/products on the menu the MainCtrl reinitialize!!
It's because the {reload:true}.

我的问题:
1) 为什么父"状态控制器也会在每次点击时重新初始化?
2) 我需要一个优雅的解决方案来避免MainCtrl重新初始化

My questions:
1) Why the "father" state controller also reinitialize on each click?
2) I need an elegant solution to avoid the MainCtrl to reinitialize!

这是完整的示例 - plunker 请查看控制台.

Here is the complete example - plunker please look at the console.

推荐答案

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

从 ui-router 0.2.14 开始,您可以将字符串作为 reload 值传递 - ui 路由器将刷新与传递的字符串及其所有子项匹配的状态.

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.

这篇关于Angular $state.go {reload: true} 不应该重新初始化抽象父控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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