分配到$ scope时得到一个奇怪的infDig错误

时间:2014-02-19 14:56:35

标签: angularjs angularjs-scope

我的控制器是:

angular.module('mean').controller('LocationController', ['$scope', '$location', '$rootScope', 'LocationService', 'UserService', 'CompanyService', '$modal', '$routeParams', function ($scope, $location, $rootScope, LocationService, UserService, CompanyService, $modal, $routeParams) {
  $rootScope.menuItem = 'locations';
  $scope.contentTemplate = '/views/location/index.html';
  $scope.locations = [];
  $scope.newLocation = {};

  $scope.index = function() {
    CompanyService.initialized.then(function() {
      var company_id = CompanyService.getCompany()._id;
      LocationService.list(company_id, $routeParams.location_parent_id).then(function(response) {
        if(response.data.status === 'ok') {
          $scope.locations = response.data.locations;
        }
      });
    });
    $scope.location_parent_id = $routeParams.location_parent_id;
  }

  $scope.addLocationModal = function() {
    $modal({
      scope: $scope,
      template: '/views/location/addLocationModal.html',
      show: true,
      animation: 'am-fade-and-scale'
    });
  }

  $scope.createLocation = function() {
    $scope.newLocation.company_id = CompanyService.getCompany()._id;
    LocationService.create($scope.newLocation).then(function(response) {
      if(response.data.status === 'ok') {
        $scope.$hide();
      }
    });
  }

}]);

在我看来,我正在做ng-init=index()

由于某种原因,行$scope.locations = response.data.locations;会导致错误,如Error: [$rootScope:infdig] http://errors.angularjs.org/1.2.14-build.2270+sha.47ec6f5/$rootScope/infdig?p0=10

如果我注释掉了作业,那么没有错误(但显然没有位置)。我做错了什么?

**我的观点**

<section ng-init="index()">
  <h1>Locations</h1>

  <div ng-show="locations.length === 0" class="well well-lg">
    You don't have any locations currently. <a href="#" ng-click="addLocationModal(location_parent_id)">Add a Location</a>
  </div>
  <div ng-show="locations.length > 0">
    <div class="row" ng-repeat="group in locations | rowgroup: 3">
      <div class="col-md-4" ng-repeat="location in group">
        <ul class="list-group">
          <li class="list-group-item">
            <h3>
              <a href="/company/locations/{{ location._id }}">
                {{ location.name }}
              </a>
            </h3>
          </li>
          <li class="list-group-item">
            <strong>Items:</strong> 2,231
          </li>
          <li class="list-group-item">Morbi leo risus</li>
          <li class="list-group-item">Porta ac consectetur ac</li>
          <li class="list-group-item">Vestibulum at eros</li>
        </ul>
      </div>
    </div>
  </div>
</section>

过滤器是:

angular.module('mean').filter('rowgroup', function() {
  /**
   * splits an array into groups of the given size
   * e.g. ([1, 2, 3, 4, 5], 2) -> [[1, 2], [3, 4], [5]]
   */
  return function(array, groupSize) {
    return _.groupBy(array, function(val, index) {
      return Math.floor(index / groupSize);
    });
  };
});

1 个答案:

答案 0 :(得分:1)

该错误是Infinite $digest Loop错误。我假设ng-init调用导致无限循环,因为它被称为正在更新所有$scope.locations

您会在文档中注意到ng-init的正确用法是ng-init="innerIndex = $index"

请参阅:http://docs.angularjs.org/api/ng/directive/ngInit