Reputation: 1092
I have got a controller named newGroupCtrl whose definition is like :
.state('new_group', {
url: '/new_group',
templateUrl: 'templates/new_group.html',
controller: 'newGroupCtrl'
})
.controller('newGroupCtrl', function ($scope, $rootScope,$ionicHistory,$window) {
$rootScope.roomId = $scope.getRoom();
$scope.getRoom = function () {
var date = new Date;
var minutes = date.getMinutes();
var hour = date.getHours();
return 'room_' + hour + '' + minutes;
};
}
I reach this contoller from previous page by :
$window.location.href = ('#/new_group');
That's good until now. $rootScope.roomId
variable is initialized in the newGroupCtrl controller properly.
From this new_group page, I navigate to another page. And when I navigate back to this page by calling $window.location.href = ('#/new_group');
,
$rootScope.roomId
is not initialized again, instead its old value is still there. The state of the newGroupCtrl is preserved.
How can I completely reinitialize newGroupCtrl?
Upvotes: 5
Views: 6544
Reputation: 52
Also I found helpful (for Ionic Framework) to use
.state('new_group', {
url: '/new_group',
templateUrl: 'templates/new_group.html',
cache: false
})
Reference to similar question and problems: Reloading current state - refresh data or in sjm's answer in Reinitialize Controller every time when visiting View - Ionic Framework
Upvotes: 0
Reputation: 1776
Since you are using Ionic Framework (Good Job), you can do this:
.controller('YourCtrl', function($ionicView){
$ionicView.enter(function(){
//code that you want to run, each time the view is active
});
});
Upvotes: 2
Reputation: 136154
You need to tell state
that reload controller each time when URL is getting accessed via browser by just adding adding reload
option of state to true
like reload: true
.
Code
.state('new_group', {
url: '/new_group',
templateUrl: 'templates/new_group.html',
controller: 'newGroupCtrl',
reload: true //will reload controller when state is being access
});
You should use $state.go('new_group')
instead of doing $window.location.href = ('#/new_group');
which will ensure that the route changes will recognize by ui-router
.
Same SO answer here
Upvotes: 5
Reputation: 542
Remove the controller from :
.state('new_group', {
url: '/new_group',
templateUrl: 'templates/new_group.html',
})
And add the controller at "new_group.html" page with parent tag of the page like:
<div ng-controller="newGroupCtrl"></div>
Upvotes: 0