I’ve just updated to 1.0.0 from 1.0.0-beta.13. I’d noticed that views were not updating when the state was changed. This was easily resolved by setting cache to false in the $stateProvider config. So the below will ensure that the controller logic will be executed:
.state('app', {
url: "/app",
cache: false,
abstract: true,
templateUrl: "templates/menu.html",
controller: 'MenuCtrl'
})
As a secondary issue, I am changing view state in parts of my app by programatically updating the window.location (note NOT $window). This used to change the view and invoke the controller logic, but now only switches to a cached view, which is not what I want. Is this a bug? or is it expected behaviour? I assume it has something to do with how the caching has been implemented.
And yes, I know I can refactor huge swathes of code and use $state.transitionTo, but I’d really rather not.
Any ideas?
Okay, so I’m assuming this is a bug.
As a workaround, I’ve placed all the controller logic that must not be cached in an $ionicView.beforeEnter event. e.g.
var MyCtrl = function($scope) {
$scope.$on('$ionicView.beforeEnter', function() {
// non-cached logic here
$scope.val1 = globalVal ? "foo" : "bar";
});
// cached logic here
$scope.val2 = "foobar";
}
1 Like
+1 on this.
Although I’m using state.go (that underneats uses transition.to) I need some kinda of way for removing a view from cache so the controller executes again when I navigate to it.
clearCache() doesn’t seem to works with abstracted states and neither does removes from cache the current state. I’m gonna have to do beforeEnter hack too.
+1. We need a way to force refresh the cached view.
You can disable the cache globally:
$ionicConfigProvider.views.maxCache(0);
Make sure to always use ‘ui-sref’ or $state.go:
<a ui-sref="app.editor({postId: item.post.id})">Go!</a>
or:
$state.go('app.editor', {postId: item.post.id});
And, not ‘href’.
Use $ionicView.beforeEnter or $stateChangeSuccess to update your cached views:
$scope.$on('$ionicView.beforeEnter', function() {
// Update your models
$log.info('$ionicView.beforeEnter');
});
See: http://ionicframework.com/docs/api/directive/ionNavView/
I don’t want to disable cache globally. This is specially useful in infinite scroll list where accessing an element and going back (keep the scroll position and everything loaded). But if I want to refresh the list, I should also be able too.
And it’s simpler to call $state.reload() instead of doing the watch on $ionicView.beforeEnter for every single controller.