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);
};
});
phuzi
  • 12,078
  • 3
  • 26
  • 50
Ashlin K Siby
  • 23
  • 1
  • 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. – Patrick McElhaney Mar 23 '18 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 – Andrew Adam Mar 23 '18 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. – Ashlin K Siby Mar 23 '18 at 19:18
  • @AndrewAdam I think this was it now it works fine . Thanks a lot. – Ashlin K Siby Mar 23 '18 at 19:20
  • @AshlinKSiby I have added my answer as an answer for further wanderers seeking advice. Happy to help :) – Andrew Adam Mar 29 '18 at 14:04

2 Answers2

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.

Andrew Adam
  • 1,572
  • 9
  • 22
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.

Jesus Duran
  • 333
  • 2
  • 14