angularJs 页面定时刷新

angularJs 页面定时刷新

页面定时刷新并在页面离开时停止自动刷新

复制代码
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();

};

 

上面是我在项目开发中用到的刷新页面的常用方式,以后用到了其他刷新页面的方式,再总结于此。

posted @ 2017-03-23 10:07  Lonely,lonelyBurning  阅读(3728)  评论(0编辑  收藏  举报