为什么第一个总是在$ rootScope上出现问题。$ apply?

时间:2014-02-03 16:22:53

标签: angularjs

我有这样的itteration,它为服务中的rootScope添加了一些值:

$rootScope.language = {};

phraces.forEach(function(phrace){
    $rootScope.$apply(function () {
        $rootScope.language[phrace.name] = phrace.value;
    });
});

第一次尝试时出现以下错误:

Error: [$rootScope:inprog] http://errors.angularjs.org/1.2.10/$rootScope/inprog?p0=%24digest
    at Error (native)
    at file:///Users/kristoffer.nolgren/web/spreadsheet2angular/js/vendor/angular.min.js:6:450
    at m (file:///Users/kristoffer.nolgren/web/spreadsheet2angular/js/vendor/angular.min.js:96:353)
    at h.$apply (file:///Users/kristoffer.nolgren/web/spreadsheet2angular/js/vendor/angular.min.js:103:31)
    at file:///Users/kristoffer.nolgren/web/spreadsheet2angular/js/main.js:28:17
    at Array.forEach (native)
    at file:///Users/kristoffer.nolgren/web/spreadsheet2angular/js/main.js:27:13
    at file:///Users/kristoffer.nolgren/web/spreadsheet2angular/js/vendor/angular.min.js:66:351
    at C (file:///Users/kristoffer.nolgren/web/spreadsheet2angular/js/vendor/angular.min.js:92:375)
    at C (file:///Users/kristoffer.nolgren/web/spreadsheet2angular/js/vendor/angular.min.js:92:375) 

其余的itterations工作得很干净!

全js:

angular.module('spreadsheet2angular', []).
    controller('Language', function(spreadsheet2angular){

    }).
    service('spreadsheet2angular', ['$http', '$rootScope',
        function($http, $rootScope){

        $http({method: 'GET', url: 'http://cors.io/spreadsheets.google.com/feeds/cells/0Aq_23rNPzvODdFlBOFRYWlQwUFBtcXlGamhQeU9Canc/od6/public/values?alt=json'}).
            success(function(data, status, headers, config) {
                $rootScope.language = {};
                var entries = data.feed.entry;
                var phraces = [];
                entries.forEach(function(entry){
                    var cell =  entry.gs$cell;
                    if(!phraces[cell.row]){
                        phraces[cell.row] = {};
                    }
                    if(cell.col == 1)
                    {
                        phraces[cell.row].name = cell.$t;
                    }
                    else if(cell.col == 2)
                    {
                        phraces[cell.row].value = cell.$t;
                    }
                });
                phraces.forEach(function(phrace){
                    $rootScope.$apply(function () {
                        $rootScope.language[phrace.name] = phrace.value;
                    });
                });
                console.log($rootScope.language);
            }).
            error(function(data, status, headers, config) {
                console.log('error');
            });
        }]);

1 个答案:

答案 0 :(得分:2)

尝试在没有$ apply的情况下运行它:

phraces.forEach(function(phrace){
  $rootScope.language[phrace.name] = phrace.value;
});

$ http已在解决后触发摘要,请参阅source code

function done(status, response, headersString) {

    // some code

    if (!$rootScope.$$phase) $rootScope.$apply(); // yeah!
  }