问题描述
我想一个网页,$范围的一部分上更新一些文本。不过,我不断收到此错误:
I'm trying to update some texts on a page that is part of $scope. But I keep getting this error:
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)
很显然,我做错了什么。 :)
我如何才能解决这个问题的任何想法?我希望页面更新到范围的新的变量。
Obviously I'm doing something wrong. :)Any ideas of how I can fix this? I want the page to update to the new variables in the scope.
这是code我使用更新:
This is the code I'm using for updating:
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为每一页我加载单独的控制器,如果有什么待办事项呢?
I'm also using ngRoute with separate controllers for each page I load, if that has anything todo with it?
推荐答案
您正在使用 $范围。$申请(...)
在函数中 changeLang
那么你所得到的共同'已经在消化的错误。你并不需要把$范围内调用 getTranslation
$申请(...)块,因为 NG-点击
已经有你的照顾。猛拉了这一点,它应该只是工作。另外,我建议你使用非缩小的版本角用于开发运行,所以您可以在控制台中看到更好的错误。
You are using $scope.$apply(...)
inside the function changeLang
so you are getting the common 'already in a digest' error. You don't need to put the call to getTranslation
inside a $scope.$apply(...) block because ng-click
already has you taken care of. Yank that out and it should just work. Also, I'd recommend running with a non-minified version of angular for dev so you can see better errors in your console.
这篇关于。angularjs $ $范围适用于()给出了这样的错误:错误:[$ rootScope:inprog]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!