Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

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:

<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?

like image 231
just_user Avatar asked Apr 17 '14 08:04

just_user


People also ask

What is rootScope apply ()?

$scope.$apply() This function is used to execute an expression in Agular. The function expression is optional and you can directly use $apply(). This is used to run watcher for the entire scope. $rootScope.$digest()

What is rootScope in AngularJS?

All applications have a $rootScope which is the scope created on the HTML element that contains the ng-app directive. The rootScope is available in the entire application. If a variable has the same name in both the current scope and in the rootScope, the application uses the one in the current scope.

What is the difference between scope and rootScope in AngularJS?

The main difference is the availability of the property assigned with the object. A property assigned with $scope cannot be used outside the controller in which it is defined whereas a property assigned with $rootScope can be used anywhere.

How many rootScope can Angular JS application have?

An app can have only one $rootScope which will be shared among all the components of an app.


2 Answers

If your template don't change after changing models and you need using $scope.$apply, You can use $scope.$applyAsync instead of this.

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

like image 179
M.javid Avatar answered Oct 17 '22 04:10

M.javid


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.

like image 14
SonOfNun Avatar answered Oct 17 '22 02:10

SonOfNun