Error Angularjs

Asked

Viewed 194 times

-1

Hello,

I am applying the following function in my Angularjs controller:

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

However, when running $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)

someone knows what can be?

1 answer

3

Probably the error of your code is that $apply takes as parameter a function to be executed.

So the correct way to write this code would be:

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

However, I found it strange your need for a $apply in this short example. 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’s the case, you’ll get a new error (other than the first) saying that Digest is already happening and that the $apply call is redundant.

UPDATE

I actually read your error fast. If you paste the link it shows you in the error (http://errors.angularjs.org/1.2.15/$rootScope/inprog? P0=%24apply) you will see a page of the angular documentation explaining the problem better. In this case, it is actually possible for $apply to be invoked without an expression. The real problem is that Digest is already happening and your call to $apply is redundant.

So just remove her that everything should work fine.

  • The problem is that when I only do $Scope.name = $Scope.namebackup it doesn’t update my view, it just updates my Scope @loreno-oliveira

  • You can edit the topic and add html?

  • I was able to solve it but only by removing the parentheses from the :D application

Browser other questions tagged

You are not signed in. Login or sign up in order to post.