问题描述
我有一个名为newGroupCtrl其定义为像控制器:
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;
};
}
我伸手从previous本页面位指示:
I reach this contoller from previous page by :
$window.location.href = ('#/new_group');
这是很好的,直到如今。 $ rootScope.roomId
变量在newGroupCtrl控制器正确初始化。
That's good until now. $rootScope.roomId
variable is initialized in the newGroupCtrl controller properly.
从这个new_group页,我导航到另一个页面。当我通过调用 $ window.location.href =('#/ new_group')导航回到此页;
,
$ rootScope.roomId
未再次初始化,而不是它的旧值仍然存在。该newGroupCtrl的状态是preserved。
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.
如何才能完全重新初始化newGroupCtrl?
How can I completely reinitialize newGroupCtrl?
推荐答案
您需要告诉状态
每次当URL是越来越通过浏览器只需添加访问时重新加载控制器加国重新加载
选项真正
如重载:真的
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
});
您应该使用 $ state.go('new_group')
而不是做 $ window.location.href =('#/ new_group );
这将确保路由变化都会通过 UI路由器
识别
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
.
SO回答 此处
这篇关于AngularJS重新初始化控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!