Search code examples
angularjsrestangular

Restangular get response errors


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


Solution

  • 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