angularjsangularjs-scopeparse-javascript-sdkangularjs-apply

$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);
};
});

Solution

  • 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.