Angular ErrorJS

-1

Hello,

I'm applying the following function in my AngularJS controller:

$scope.cancelChanges = function() {
        $scope.name = $scope.namebackup;
        $scope.$apply();
    };

However, when I run $ apply (), it shows me the following error:

Error: [$rootScope:inprog] http://errors.angularjs.org/1.2.15/$rootScope/inprog?p0=%24apply
    at Error (native)
    at http://local/vendor/bower/angular/angular.min.js:6:450
    at m (http://local/vendor/bower/angular/angular.min.js:101:443)
    at h.$apply (http://local/vendor/bower/angular/angular.min.js:108:301)

Does anyone know what it can be?

    
asked by anonymous 07.04.2014 / 15:15

1 answer

3

Probably the error in your code is that $ apply receives a function as a parameter to execute.

So the correct way to write this code would be:

$scope.cancelChanges = function() {
    $scope.$apply( function() {
        $scope.name = $scope.namebackup;
    } );
};

However, I found your need for a $ apply in this short example strange. Unless the cancelChanges () function is being invoked from some JS code that is not part of the angular app (a JQuery plugin for example), then $ apply is redundant. And if that is the case, you will get a new error (different from the first) saying that the digest is already happening and that the $ apply call is redundant.

UPDATE

I actually read your mistake fast. If you paste the link it shows you in error ( link $ rootScope / inprog? P0 =% 24apply) you'll see a page of the angular documentation explaining the problem better. In this case, it is actually possible that $ apply is invoked without an expression. The real problem is that the digest is already happening and your call to $ apply is redundant.

So just remove it so everything should work fine.

    
08.04.2014 / 00:03