Hello, to everyone.
I'm a student in IT and i'm doing my internship. I've searched for a guide
on how to use $resource service with $q and defer, a lot of stuff there,
but seams that my "result" is too unexpected. Anyway, my task is to take
data from a restful service and if i use only $resource i haven't problems,
all work fine. But i want improve my code with promises and defer. This is
how i organize the work:
1) Create a ServiceA that use $resource to get the data from server
2) Create a ServiceB that with the ServiceA create a promise and give me
the data if they are avaiable
3) Create a Controller that use ServiceB for bind the data in to the $scope.
var modulo = angular.module('starter.services', ['ngResource']);
modulo.factory("serviceModality", ['$resource', '$q','serviceSettings',
function( $resource, $q,serviceSettings)
{
var objFactory = {};
objFactory.risposta = function(riposta)
{
var variabile =
$resource(serviserviceSettingsceImpostazioni.getURLlistaModality()), defObj
= $q.defer();
var query1 = variabile.query();
query1.$promise.then(function(risposta)
{
defObj.resolve(risposta);
},
function(error)
{
defObj.resolve("Errore, server
offline!!");
});
return defObj.promise;
}
return objFactory;
}]);
modulo.factory('serviceSettings', function()
{
var urlAutenticazione =
"http://localhost:8080/ServerTomCat/json/product/app/";
var urlListaModality =
"http://localhost:8080/ServerTomCat/json/product/app/modality";
var urlListaPazienti = "";
var fattoria = {};
fattoria.getURLautenticazione = function(){
return urlAutenticazione;
}
fattoria.getURLlistaModality = function(){
return urlListaModality;
}
fattoria.getURLpazienti = function(){
return urlListaPazienti;
}
return fattoria;
});
modulo.controller('NewControllerModality', ['$scope', 'serviceModality',
function ($scope, serviceModality)
{
$scope.datiRicevuti = {};
serviceModality.risposta($scope.datiRicevuti);
console.log($scope.datiRicevuti);
}]);
In my console i have 3 Objects , for me are unexpected. What i expect is my
data or "error message" .
Can someone help me with this?
Thanks
--
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.