AngularJS 重新初始化控制器
AngularJS reinitialize controller
我有一个名为 newGroupCtrl 的控制器,其定义如下:
.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;
};
}
我通过以下方式从上一页到达此控制器:
$window.location.href = ('#/new_group');
到目前为止还不错。 $rootScope.roomId
变量在 newGroupCtrl 控制器中正确初始化。
从这个 new_group 页面,我导航到另一个页面。当我通过调用 $window.location.href = ('#/new_group');
导航回此页面时,
$rootScope.roomId
没有再次初始化,而是它的旧值仍然存在。 newGroupCtrl 的状态被保留。
如何完全重新初始化 newGroupCtrl?
从 :
移除控制器
.state('new_group', {
url: '/new_group',
templateUrl: 'templates/new_group.html',
})
并在 "new_group.html" 页面添加控制器,页面的父标签如下:
<div ng-controller="newGroupCtrl"></div>
您需要告诉 state
每次通过浏览器访问 URL 时重新加载控制器,只需将 reload
状态选项添加到 true
,例如 reload: true
.
代码
.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-router
识别。
这里也是
由于您使用的是 Ionic Framework(干得好),您可以这样做:
.controller('YourCtrl', function($ionicView){
$ionicView.enter(function(){
//code that you want to run, each time the view is active
});
});
我还发现(对于 Ionic Framework)使用起来很有帮助
.state('new_group', {
url: '/new_group',
templateUrl: 'templates/new_group.html',
cache: false
})
参考类似问题:
Reloading current state - refresh data or in sjm's answer in
我有一个名为 newGroupCtrl 的控制器,其定义如下:
.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;
};
}
我通过以下方式从上一页到达此控制器:
$window.location.href = ('#/new_group');
到目前为止还不错。 $rootScope.roomId
变量在 newGroupCtrl 控制器中正确初始化。
从这个 new_group 页面,我导航到另一个页面。当我通过调用 $window.location.href = ('#/new_group');
导航回此页面时,
$rootScope.roomId
没有再次初始化,而是它的旧值仍然存在。 newGroupCtrl 的状态被保留。
如何完全重新初始化 newGroupCtrl?
从 :
移除控制器.state('new_group', {
url: '/new_group',
templateUrl: 'templates/new_group.html',
})
并在 "new_group.html" 页面添加控制器,页面的父标签如下:
<div ng-controller="newGroupCtrl"></div>
您需要告诉 state
每次通过浏览器访问 URL 时重新加载控制器,只需将 reload
状态选项添加到 true
,例如 reload: true
.
代码
.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-router
识别。
这里也是
由于您使用的是 Ionic Framework(干得好),您可以这样做:
.controller('YourCtrl', function($ionicView){
$ionicView.enter(function(){
//code that you want to run, each time the view is active
});
});
我还发现(对于 Ionic Framework)使用起来很有帮助
.state('new_group', {
url: '/new_group',
templateUrl: 'templates/new_group.html',
cache: false
})
参考类似问题:
Reloading current state - refresh data or in sjm's answer in