I'm trying to update some texts on a page that is part of $scope. But I keep getting this error:
我正在尝试更新作为$ 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)
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.
显然我做错了。 :)关于如何解决这个问题的任何想法?我希望页面更新到范围中的新变量。
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));
});
});
};
});
the html:
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.
...
I'm also using ngRoute with separate controllers for each page I load, if that has anything todo with it?
我也在为我加载的每个页面使用ngRoute和单独的控制器,如果它有任何东西吗?
3 个解决方案
#1
9
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.
你在函数changeLang中使用$ scope。$ apply(...),这样你就会得到常见的“已经在摘要中”的错误。你不需要在$ scope。$ apply(...)块中调用getTranslation,因为ng-click已经由你处理了。突然说,它应该工作。另外,我建议使用非缩小版本的angular for dev,这样你就可以在控制台中看到更好的错误。
#2
16
If your template don't change after changing models and you need using $scope.$apply
, You can use $scope.$applyAsync
instead of this.
如果您的模板在更改模型后没有更改,并且您需要使用$ scope。$ apply,您可以使用$ scope。$ applyAsync而不是此。
https://github.com/angular-ui/ui-codemirror/issues/73
https://github.com/angular-ui/ui-codemirror/issues/73
#3
0
$evalAsync works great:
$ evalAsync效果很好:
$scope.$evalAsync(function() {
// Code here
});
Or simply:
或者干脆:
$scope.$evalAsync();
See: https://docs.angularjs.org/api/ng/type/$rootScope.Scope#$evalAsync
请参阅:https://docs.angularjs.org/api/ng/type/$rootScope.Scope#$evalAsync
#1
9
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.
你在函数changeLang中使用$ scope。$ apply(...),这样你就会得到常见的“已经在摘要中”的错误。你不需要在$ scope。$ apply(...)块中调用getTranslation,因为ng-click已经由你处理了。突然说,它应该工作。另外,我建议使用非缩小版本的angular for dev,这样你就可以在控制台中看到更好的错误。
#2
16
If your template don't change after changing models and you need using $scope.$apply
, You can use $scope.$applyAsync
instead of this.
如果您的模板在更改模型后没有更改,并且您需要使用$ scope。$ apply,您可以使用$ scope。$ applyAsync而不是此。
https://github.com/angular-ui/ui-codemirror/issues/73
https://github.com/angular-ui/ui-codemirror/issues/73
#3
0
$evalAsync works great:
$ evalAsync效果很好:
$scope.$evalAsync(function() {
// Code here
});
Or simply:
或者干脆:
$scope.$evalAsync();
See: https://docs.angularjs.org/api/ng/type/$rootScope.Scope#$evalAsync
请参阅:https://docs.angularjs.org/api/ng/type/$rootScope.Scope#$evalAsync