頁面定時刷新並在頁面離開時停止自動刷新
var autoRefresh; //自動刷新 autoRefresh = $interval($scope.loadData, 60000); //停止自動刷新 $scope.stopAutoRefresh = function () { if (autoRefresh) { $interval.cancel(autoRefresh); autoRefresh = null; } }; //切換頁面時停止自動刷新 $scope.$on('$routeChangeStart', function (angularEvent, current, previous) { $scope.stopAutoRefresh(); });
.state('test', {
url: '/test',
cache:'false',
templateUrl: 'templates/test.html',
controller: 'testCtrl'
})
$state.go("xxx", {}, { reload: true });
.state('test', {
url: '/test',
templateUrl: 'templates/test.html',
controller: 'testCtrl'
})
.controller('testCtrl', function($scope,$state,$window) {
$scope.loadData= function() {
alert(123);
}
$scope.loadData();
})
3、
AngularJs 刷新頁面可采用下面的方式:首先先在控制器中注冊$window,然后定義函數$scope.reloadRoute,在需要刷新頁面的地方調用函數$scope.reloadRoute即可。
$scope.reloadRoute = function () {
$window.location.reload();
};
上面是我在項目開發中用到的刷新頁面的常用方式,以后用到了其他刷新頁面的方式,再總結於此。