Angular $ state.go {reload:true}不应该重新初始化抽象父控制器 [英] Angular $state.go {reload: true} shouldn't reinitialize the abstract parent controller
问题描述
我正在使用ui-route进行导航.
我有一个称为 main 的父亲州,它是一个抽象
状态(网址:/main ),而子状态是产品和用户(网址:/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.
推荐答案
- 将您的ui路由器更新为较新的版本(至少为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
值传递-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屋!