Ashlin K Siby
Ashlin K Siby

Reputation: 23

$scope.$apply() not working - AngularJs

The value of isV(boolean) is not changing despite applying $scope.$apply() after the user enters the correct username and password. $scope.$digest() is also not working

app.controller('validateCtrl', function($scope,$location) {

Parse.initialize("PARSE APP ID");
Parse.serverURL = 'PARSE SERVER URL'


$scope.isV="false";

$scope.submit = function(){

    var username =$scope.username;
    var password = $scope.password;

    Parse.User.logIn(username, password, {
        // If the username and password matches
        success: function(user) {
            alert('Welcome!');
            console.log("welcome");

            $scope.$apply(function(){
                  $scope.isV="true";      
            });

        },
        // If there is an error
        error: function(user, error) {
            alert(error);
        }
    });
 console.log($scope.isV);
};
});

Upvotes: 0

Views: 3876

Answers (2)

Andrew Adam
Andrew Adam

Reputation: 1582

For further reference my comment as an answer: 1) Read Patrick's comment first on the post 2) The solution: There is no need to set the isV value within the apply function but you call it after, like this:

$scope.isV = true;
$scope.$apply();

Additionally the console.log() should also be within the success otherwise it will be called undeterministically - probably before the query succeeds.

Upvotes: 1

Jesus Duran
Jesus Duran

Reputation: 333

Try using this instead of $scope.$apply():

$timeout(function() {
     $scope.isV = "true";
});

You will have to inject $timeout in your controller to make this work.

Upvotes: 0

Related Questions