1

I'm using parse with angularjs to authenticate users. Here is the login function.

$scope.doLogin = ->
    Parse.User.logIn $scope.currentUser.username, $scope.currentUser.password,
      success: (user) ->
        console.log user
        $scope.currentUser = user
      error: (user, error) ->
        console.log error

And here is the form (used twice in same page, navbar dropdown and in page content):

%form{"ng-submit" => "doLogin()"}
  %input{"ng-model" => "currentUser.username", type: "text"}
  %input{"ng-model" => "currentUser.password", type: "password"}

  %button.btn.btn-block
    %center Connexion

The problem is that whenever the form is submitted, I can see the user object in console, but $scope.currentUser doesn't always get updated. Sometimes I have to submit the form 3 or 4 times in a row for it to get updated.

What am I doing wrong ? Thank you.

Alex
  • 2,036
  • 4
  • 22
  • 31

1 Answers1

1

perhaps you should include $scope.apply() in your success callback. From the Angularjs docs:

"$apply() is used to execute an expression in angular from outside of the angular framework. (For example from browser DOM events, setTimeout, XHR or third party libraries). Because we are calling into the angular framework we need to perform proper scope life-cycle of exception handling, executing watches."

I had a similar problem with Parse and I'd solved it with $scope.apply().

Here is my controller

ps. note that I'm relatively new to angularjs and the above code may not be the most efficient :)