0

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);
};
});
5
  • 1
    Hi Ashlin, thanks for the question. When you say $scope.$apply() is not working, I'm sure what you mean is "$scope.$apply() is not working as I expected." That's completely normal and why this site exists. I run into code not working as I expected several times a day at least. In order to answer your question, we need some more information. What is your current understanding of how $scope.$apply() works? What are you expecting to happen here? What's actually happening? Hope this helps. Commented Mar 23, 2018 at 12:41
  • 3
    (^what Patrick said) additionally you do not have to set the isV value within the apply function but rather after each other " $scope.isV=true;$scope.$apply(); ". Furthermore your console.log should also be within the success, after the apply() function given its asynchronous nature Commented Mar 23, 2018 at 13:14
  • 1
    @PatrickMcElhaney According to me if i use a third party library in angular it dosent work and i found some answers related to this. PS: This was my first post her i will be more precise next time. Thanks for the reply. Commented Mar 23, 2018 at 19:18
  • @AndrewAdam I think this was it now it works fine . Thanks a lot. Commented Mar 23, 2018 at 19:20
  • @AshlinKSiby I have added my answer as an answer for further wanderers seeking advice. Happy to help :) Commented Mar 29, 2018 at 14:04

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

What is the difference between the apply call with and without function?
0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.