0

I have a simple auth factory in angular with the following codes

.factory 'User',($window,$rootScope,Restangular)->
    {
        auth: (username,password)->
            Restangular.all 'auth/login/'
            .post {username:username,password:password}
            .then (response)->
                return response
            ,(response) ->
                return response
        .....

and i am calling that from my controller using

$scope.login = ()->
    $scope.running = 1
    console.log User.auth $scope.username,$scope.password
    $scope.running = 0
    return

The problem is that i need to show the errors on login if any and set them in controller but restangular being async, i am not sure how to do to same. As you can see from the first code segment that i tried to return the values but even they don't seem to work.

How can i get the reponse codes from the server in scope using Restangular

1 Answer 1

2

You should return the restangular call, which is a promise. Then on the callbacks you can do something with the errors:

auth: (username,password)->
    return Restangular.all 'auth/login/'
    .post {username:username,password:password}

controller:

$scope.login = ()->
    $scope.running = 1
    User.auth( $scope.username,$scope.password)
    .then (response)->
        // success
    ,(response)->
        // fail
    .finally (response)->
        $scope.running = 0
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks a lot, i was looking for something to avoid this. Anyway the code works now :)

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.