Hi Daniele.

Try to *reject* the *deferred* where you have the comments. See 
this: https://code.angularjs.org/1.2.28/docs/api/ng/service/$q#the-deferred-api
BTW, I don't think you need to redefine the *success* function of the 
created *promise*.

I hope this helps.

Regards,
*Luís Loureiro*


segunda-feira, 13 de Julho de 2015 às 07:30:26 UTC+1, Daniele Ratti 
escreveu:
>
> Hi there, I'm using the code below in order to simplify the backend 
> requests but I didn't catch how to call either a success method or an error 
> method.
> How can I reach the expected behavior commented in the code?
>  
>
>> app.factory('REST', function ($http, $q, sweetAlert) {
>>  
>>     return {
>>         load: function (module, action, data) {
>> var deferred = $q.defer();
>> var promise = deferred.promise; 
>>             $http
>>             .post('/api/'+module+'.php?action='+action, data)
>>             .success(function (data) {
>>                 
>>                 if(data.error)
>>               {
>>               sweetAlert.swal({
>>             title: "Error",
>>             text: data.error,
>>             type: "warning"
>>         });
>> * //HERE I WANT TO CALL .error(details)*
>>     }
>>     else
>>      deferred.resolve(data.result);
>>               
>>                         }).error(function () {
>> * //HERE I WANT TO CALL .error(details)*
>> });
>>  promise.success = function(fn) {
>>         promise.then(fn);
>>         return promise;
>>     }
>>      return promise;
>>         }
>>     };
>>     
>> });
>>
>
> This is the code which uses the code above:
>
>
>> $scope.login = function () {
>
> $scope.loading = true;
>
> var payload = {'credentials': $scope.logindata};
>
> REST.load('access', 'login', payload).success(function(data) {
>
> if(data.redirect)
>
> $state.go(data.redirect);
>
> $scope.loading = false;
>
> *}).error(function(data) { //THIS SHOULD BE CALLED*
>
> * $scope.loading = false;*
>
> * });*
>
> }
>
>  
>
>
> Thank you very much 
>

-- 
You received this message because you are subscribed to the Google Groups 
"AngularJS" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/angular.
For more options, visit https://groups.google.com/d/optout.

Reply via email to