【问题标题】:angular timeout reload after page change页面更改后角度超时重新加载
【发布时间】:2016-06-23 10:32:14
【问题描述】:

我使用 angular $time out 从 api 重新加载数据并在 dom 中显示实时统计数据。重新加载效果很好,5秒后重新加载。但问题是,在我点击另一个选项卡后,我不需要 $timeout 并重新加载数据,仍然像之前的页面一样重新加载数据。该数据不在 DOM 中,但在网络控制台中仍从以下代码重新加载数据,并使用该数据调用 http get url。

$scope.reload = function () {
$http.get(serviceBase + 'live-stats').
    success(function (res) {
         $scope.proizvedeno = res;
console.log(res);  
         });
         $timeout(function(){
            $scope.reload();
            }, 5000);
    };
            $scope.reload(); 

附:我使用 angular-loading-bar,有没有办法让这个栏在重新加载页面的某个部分时不旋转,在这种情况下,从“超时”中检索数据以显示统计信息

编辑: 我将此添加到此 ctrl

$scope.reload = function () {
$http.get(serviceBase + 'live-stats').
    success(function (res) {
         $scope.proizvedeno = res;
console.log(res);  
         });
         $timeout(function(){
            $scope.reload();
            }, 5000);
 $scope.$on('$destroy', function(){    //this is what i add
$timeout.cancel(reload);             //this is what i add
    };
            $scope.reload(); 

但超时仍未取消

【问题讨论】:

    标签: angularjs timeout


    【解决方案1】:

    我只需要声明“var reload”

    var reload = $timeout(function(){
                $scope.reload();
                }, 5000);
                $scope.$on('$destroy', function(){
    $timeout.cancel(reload);
    

    });

    【讨论】:

      猜你喜欢
      • 2016-03-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-03-29
      • 2017-10-22
      • 2019-07-15
      • 2012-05-21
      • 2013-12-22
      相关资源
      最近更新 更多