Javascript $route.reload();问题

Javascript $route.reload();问题,javascript,angularjs,Javascript,Angularjs,Hi im currenty在每次更新数据库时使用$route.reload刷新控制器的内容。问题是在更新大量数据列表时,每次我更新数据库并运行$route.reload my browser(重新加载我的浏览器)时都会失去向上或向下滚动浏览器的功能,因此它可以处理较小的数据列表 下面是我的代码示例 $scope.Undone = function(id){ $scope.index = $scope.GetID ; CRUD.put('/UndoJda/'+$scope.i

Hi im currenty在每次更新数据库时使用$route.reload刷新控制器的内容。问题是在更新大量数据列表时,每次我更新数据库并运行$route.reload my browser(重新加载我的浏览器)时都会失去向上或向下滚动浏览器的功能,因此它可以处理较小的数据列表

下面是我的代码示例

$scope.Undone = function(id){
    $scope.index =  $scope.GetID ;

    CRUD.put('/UndoJda/'+$scope.index).then(function(response){         
            toastr.info('Jda has been activated.', 'Information');
    $route.reload();
    });
}

当数据量很大时,尝试使用
$timeout
并重新加载页面

这将防止非常快的刷新,并将保持您的页面响应

$scope.Undone = function(id){
    $scope.index =  $scope.GetID ;

    CRUD.put('/UndoJda/'+$scope.index).then(function(response){         
        toastr.info('Jda has been activated.', 'Information');
        $timeout(function() {
          $route.reload();
        }, 200);
    });
}

最好的选择是某种延迟加载/分页。因此,如果它是一个非常大的列表,比如在成千上万个列表中,它甚至可能是一个DOM渲染问题。此外,如果不是这样,您应该尝试使用AngularJS的bind once(从1.3开始提供),以及track,它不会在模板中为作用域上的每个对象创建观察者。假设您使用的是ngRepeat,让我们这样说:

...<ul>
    <li ng-repeat="item in Items">
      <b>{{item.name}}</b>
    </li>
   </ul>
  • {{item.name}
如果数据不经常更新,请将其更改为以下内容:

...<ul>
    <li ng-repeat="item in Items track by $index">
      <b>{{::item.name}}</b>
    </li>
   </ul>
  • {{::item.name}

作为旁注,试着在模特的名字中总是有一个点$scope.Something.list,例如。(“如果你没有一个点,你就做错了”-米斯科·海弗利自己这么说。)

你可以使用
$interval

 $interval(function() {
     CRUD.put('/UndoJda/'+$scope.index).then(function(response){         
        toastr.info('Jda has been activated.', 'Information');  
        // Update scope variable 
      });
    }, 2000);

也不要使用
$route.reload()。因为Angularjs支持SPA(单页应用程序)。如果使用
$route.reload()。每次页面都会加载,所以不好。您只需在间隔内调用
服务代码。

首先,我建议删除$route.reload()的用法,您的用例不需要视图重新实例化控制器。相反,您应该更新保存视图中呈现的实体集合的$scope变量。您还需要考虑添加诸如加载指示器之类的UX特性来通知用户关于长时间运行的任务。

类似的东西,下面的代码将实现你的期待。我不知道您的CRUD js对象实例是什么,但只要它能够感知角度,您就不需要使用$timeout。Angular aware通常意味着非第三方API,但您可以使用$q帮助将第三方ajax结果公开给Angular

// angular module.controller()
function Controller($scope, EntityService) {
    $scope.entityCollection = [];
    $scope.loadingData = false; // used for loading indicator 

    // Something will initialize the entity collection
    // this is however your already getting the entity collection
    function initController() {
        $scope.refreshCollection();
    }

    initController();

    $scope.refreshCollection = function() {
        $scope.loadingData = true;

        EntityService.getEntitites().then(function(resp) {
            $scope.entityCollection = resp;

            $scope.loadingData = false;
        });
    }

    $scope.Undone = function(id) {
        $scope.index =  $scope.GetID ;

        CRUD.put('/UndoJda/' + $scope.index).then(function(response){         
            toastr.info('Jda has been activated.', 'Information');

            $scope.refreshCollection();
        });
    }
}

// angular module.factory()
function EntityService($q, $http) {
    return {
        getEntitites: function() {
            var deferred = $q.defer();

            $http.post('/some/service/endpoint').then(function(resp) {
                deferred.resolve(resp);
            });

            return deferred.promise;
        }
    }
}

当数据量巨大时,在特定延迟后重新加载如何?我还没有尝试过,但我会试一试你的解决方案完全有效!!太多了!关于在
$http
中使用它,您能更具体一些吗?我已经做了一些类似的事情,我遇到了一些问题,所以我决定改用$route.reload。@BananaTiger您能提供有关遇到的问题的详细信息吗?我认为从长远来看,您可能会发现$route.reload会出现问题,这取决于您在UI中的操作。