I am using ui-route for navigation.
I have the state of father main , this state is abstract (url: / main) and the child states are products and users (URL: / 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", }) } ]);
And here are 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") });
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 use: $ state.go ("main.products", {}, {reload: true}) to navigate
When I click on user / products in the MainCtrl menu, reinitialize !
This is because {reload:true} .
My questions:
1) Why does the father status controller also reinitialize every click?
2) I need an elegant solution to avoid MainCtrl before reinitialize !
Here is a complete example - plunker , please look at the console.