【问题标题】:How to make angular.js show the update with the data object update after some delay如何让 angular.js 在延迟后显示数据对象更新的更新
【发布时间】:2014-09-01 13:04:48
【问题描述】:

我通过从 http 服务加载它们来初始化 angular.js 中的对象数组。

  <ul class="order-list" ng-repeat="catalog in catalogs">
                <li style="width:34%;" ng-show="{{catalog.ordered}}" ><span class="fa fa-money"></span> ORDERED </li>
  </ul>

   $scope.catalogs = [];
   $http({
      url: baseURL + "/orders/"+order.date + "?code="+order.code,
      method: "DELETE",
   }).success(function (data, status, headers, config) {
     $scope.catalogs = data;
           // first way
             _.each($scope.catalogs, function( catalog){
              console.log(catalog);
              catalog.ordered = true;
            })
          // second way
          setTimeout( function(){
            console.log("settimeout");
             _.each($scope.catalogs, function( catalog){
              console.log(catalog);
              catalog.ordered = true;
            })
            $scope.$apply();
          }, 2000)
   })

和代码一样,如果我使用第一种方式,那么 html 会立即显示更新,但是,如果我使用第二种方式,延迟三秒后更新,那么 html 不会显示任何更新,甚至我用 $scope.$apply();它只是给了我一个错误 错误:[$rootScope:inprog] http://errors.angularjs.org/1.3.0-beta.17/$rootScope/inprog?p0=%24digest

如何进行更新

【问题讨论】:

    标签: angularjs


    【解决方案1】:

    您是否尝试过使用$timeout 而不是setTimeout()

    $timeout(function() {
      _.each($scope.catalogs, function(catalog) {
        catalog.ordered = true;
      });
    }, 2000);
    

    【讨论】:

    • 我不认为这是 setTimeout 或标准方式 $timeout 的问题。至少 setTimeout 会在 2 秒后执行。
    猜你喜欢
    • 2021-10-10
    • 2013-02-11
    • 2015-11-11
    • 1970-01-01
    • 2012-03-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-03
    相关资源
    最近更新 更多