簡體   English   中英

angularjs $ scope。$ apply()給出了這個錯誤:錯誤:[$ rootScope:inprog]

[英]angularjs $scope.$apply() gives this error: Error: [$rootScope:inprog]

我正在嘗試更新作為$ scope一部分的頁面上的一些文本。 但我一直收到這個錯誤:

Error: [$rootScope:inprog] [http://errors.angularjs.org/1.2.15/$rootScope/inprog?p0=%24apply][1]
at Error (native)
at https://ajax.googleapis.com/ajax/libs/angularjs/1.2.15/angular.min.js:6:450
at m (https://ajax.googleapis.com/ajax/libs/angularjs/1.2.15/angular.min.js:101:443)
at h.$apply (https://ajax.googleapis.com/ajax/libs/angularjs/1.2.15/angular.min.js:108:301)
at h.$scope.changeLang (http://treenovum.es/xlsmedical/js/medical-app.js:80:16)
at https://ajax.googleapis.com/ajax/libs/angularjs/1.2.15/angular.min.js:169:382
at https://ajax.googleapis.com/ajax/libs/angularjs/1.2.15/angular.min.js:186:390
at h.$eval (https://ajax.googleapis.com/ajax/libs/angularjs/1.2.15/angular.min.js:108:40)
at h.$apply (https://ajax.googleapis.com/ajax/libs/angularjs/1.2.15/angular.min.js:108:318)
at HTMLAnchorElement.<anonymous> (https://ajax.googleapis.com/ajax/libs/angularjs/1.2.15/angular.min.js:186:372) 

顯然我做錯了什么。 :)關於如何解決這個問題的任何想法? 我希望頁面更新到范圍中的新變量。

這是我用於更新的代碼:

medicalApp.controller('MainCtrl', function($scope, $cookies, getTranslation) {
    getTranslation.get(function(data){
        $scope.translation = data;
    });

    $scope.changeLang = function (lang) {
        console.log(lang);
        $cookies.lang = lang;
        $scope.$apply(function(){
            getTranslation.get(function(data){
                $scope.translation = data;
                console.log(JSON.stringify($scope.translation));
            });
        });
    };
});

html:

<body ng-controller="MainCtrl">
    ...
            <div class="header-lang col-xs-4">
                <p>
                    <a href="#" ng-click="changeLang('de')">DE</a> | 
                    <a href="#" ng-click="changeLang('fr')">FR</a></p>
            <div>{{ translation.text }}</div>  <---- Example variable I want updated.
    ...

我也在為我加載的每個頁面使用ngRoute和單獨的控制器,如果它有任何東西嗎?

如果你的模板在更改模型后沒有改變,你需要使用$scope.$apply ,你可以使用$scope.$applyAsync而不是這個。

https://github.com/angular-ui/ui-codemirror/issues/73

你在函數changeLang中使用$scope.$apply(...) ,這樣你就會得到常見的“已經在摘要中”的錯誤。 你不需要在$ scope。$ apply(...)塊中調用getTranslation因為ng-click已經由你處理了。 突然出來,它應該工作。 另外,我建議使用非縮小版本的angular for dev,這樣你就可以在控制台中看到更好的錯誤。

$ evalAsync效果很好:

$scope.$evalAsync(function() {
    // Code here
});

或者干脆:

$scope.$evalAsync();

請參閱: https ://docs.angularjs.org/api/ng/type/ $ rootScope.Scope#$ evalAsync

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM