I don't understand how the controller of Ionic run


#1

Here my config in app.js

.state(‘app.login’, {
url: ‘/login’,
views: {
‘menuContent’: {
templateUrl: “templates/login.html”,
controller: ‘LoginCtrl’
}
}
})
.state(‘app.accountinfo’, {
url: “/accountinfo”,
views: {
‘menuContent’: {
templateUrl: “templates/AccountInfo.html”,
controller: ‘AccountInfoCtrl’
}
}
})
.state(‘app.product’, {
url: “/product/:productId”,
views: {
‘menuContent’: {
templateUrl: “templates/product.html”,
controller: ‘ProductCtrl’
}
}
})

In controller.js file, coded like this:

.controller(‘LoginCtrl’, [’$scope’,’$state’,function($scope,Json,Account_Info,$state) {
$scope.loginData = {};
if(not loged in){

}else{
  $state.go("app.accountinfo");
}
$scope.doLogin = function() {
  });
}

}])
.controller(‘AccountInfoCtrl’,[’$scope’,’$state’, function($scope,$state) {
if(not login){
$state.go(“app.login”);
}else{
});
}else{
console.log(Account_Info.LoginID);
$state.go(“app.products”);
}
});
}
}])
.controller(‘ProductsCtrl’, [’$scope’,function($scope,Json,SystemInfo) {
//do something
});
}
}])

With this code when I go “AccountInfo” page, if I haven’t loged in, I will be redirected to Login page. It’s working, but the problem is that if after I’m rediredted to Login page, I click to “Product” tab, and go to “Product” page, then I go “AccountInfo” page again, I will not be redirected to “Login” page but stay in “AccountInfo” page, and actually the “AccountInfo” controller not run.
I don’t clearly understand the mechanic of ionic, I’m new bee. So can anyone explain me about this issue?
Thank!


#2

you should read ionic documentation carefully.

By default viewCaching is enabled so your controller is only instantiated once.
After that it is cached.

If you go another time your state --> the controller is not reexecuted.
http://ionicframework.com/docs/api/directive/ionView/


#3

Thanks! I found the solution. Will try to read document successfully.