4

AngularJS(坦率地说是 JavaScript)的新手,但从我收集的信息来看,只有在 Angular 的雷达之外发生更改时才需要显式调用 $scope.$apply() 。下面的代码(从这个 plunker粘贴)让我认为这不是需要调用的情况,但这是我让它工作的唯一方法。我应该采取不同的方法吗?

索引.html:

<html ng-app="repro">
  <head> 
    ...
  </head>
  <body class="container" ng-controller="pageController">
    <table class="table table-hover table-bordered">
        <tr class="table-header-row">
          <td class="table-header">Name</td>
        </tr>
        <tr class="site-list-row" ng-repeat="link in siteList">
          <td>{{link.name}}
            <button class="btn btn-danger btn-xs action-button" ng-click="delete($index)">
              <span class="glyphicon glyphicon-remove"></span>
            </button>
          </td>
        </tr>
    </table>
  </body>
</html>

脚本.js:

var repro = angular.module('repro', []);

var DataStore = repro.service('DataStore', function() {
  var siteList = [];

  this.getSiteList = function(callback) {
    siteList = [ 
      { name: 'One'}, 
      { name: 'Two'}, 
      { name: 'Three'}];

    // Simulate the async delay
    setTimeout(function() { callback(siteList); }, 2000);
  }

  this.deleteSite = function(index) {
    if (siteList.length > index) {
      siteList.splice(index, 1);
    }
  };
});

repro.controller('pageController', ['$scope', 'DataStore', function($scope, DataStore) {
  DataStore.getSiteList(function(list) {

    $scope.siteList = list; // This doesn't work
    //$scope.$apply(function() { $scope.siteList = list; }); // This works

  });

  $scope.delete = function(index) {
    DataStore.deleteSite(index);
  };
}]);
4

2 回答 2

5
setTimeout(function() { callback(siteList); }, 2000);

此行将带您脱离 Angular 的摘要循环。你可以简单地setTimeout用 Angular 的$timeout包装器替换(你可以将它注入到你的 DataStore 服务中),你不需要$scope.$apply.

于 2015-09-11T14:51:16.237 回答
2

setTimeoutasync被视为脱离angular上下文的事件,因此它不会运行摘要循环。执行此类操作时需要手动运行它,但首选使用$timeout.

相反,angular 确实提供了一项$timeout服务,其工作方式与 of 相同,setTimeout但在执行回调函数后,它会调用$scope.$apply()

$timeout(function() { callback(siteList); }, 2000);

特别之处$timeout在于它以更安全的方式运行摘要循环。它可以保证它不会与任何当前正在运行的摘要循环发生冲突。在幕后,当您在 内部调用函数时$timeout,它会通过检查来检查是否有任何摘要循环在运行$scope.root.$$phase,如果处于digest阶段,则将该摘要循环放入队列中,并在该摘要循环完成后运行它。

于 2015-09-11T14:51:48.210 回答