Angular $ state.go {reload:true}不应该重新初始化抽象父控制器 [英] Angular $state.go {reload: true} shouldn't reinitialize the abstract father controller
问题描述
我正在使用ui-route进行导航。
我的父状态名为 main ,它是摘要
state(网址: / main )和子状态产品和用户(网址: / main / products 和 /主/用户)。
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.
推荐答案
- 将你的ui-router更新到更新的版本(至少0.2.14)
- 更改
$ state.go
调用类似这样的内容$ state.go(main.users,{} ,{reload:main.users})
- Update your ui-router to newer version (0.2.14 at least)
- Change
$state.go
call to something like this$state.go("main.users",{},{reload: "main.users"})
由于ui-router 0.2.14你可以将字符串传递为 reload
value - 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屋!