I need to make an http request through my angularjs service. It's working asynchronously, I know, but I can't catch my data.
var dataRifValues = [];
monitoraggioProcessiService.getDataRifList().then(function(response) {
angular.forEach(response, function(date, key) {
dataRifValues.push(date.dataRiferimento);
});
});
this.getDataRifList = function() {
var url = $rootScope.baseLink + "/processi/dataRif";
var deferred = $q.defer();
var list = [];
if (list.length === 0) {
var promise = ispCommunicationManager.returnPromise(
ispServiceRequest.prepareRequest(url,"GET",
{
"Content-Type" : "application/json"
}, null, null));
promise.then(function(response) { // Success callback
list = response.data;
deferred.resolve(response.data); // Resolve
}, function(response) { // Error callback
list = response;
deferred.reject(response); // Reject
util.defaultErrorHandler(list,null);
});
list = deferred.promise;
}
return list
}
I'm calling the getDataRifList function. I expect a list with some datas.
It is like it's called twice. The first time the list is empty and the second time it has data, but my function is already executed.
What is the error?
EDIT
The call works, but when I need to set the filtroDataRiferimento selected property, the dataRifValues[0] is empty. The values property instead, has the values.
var filtroDataRiferimento = {
label : "Data di riferimento",
name : "dataRiferimento",
type : "single",
values : dataRifValues,
selected : dataRifValues[0],
flagMatch : false
};
$scope.filtraMonitoraggioProcessi(filtroDataRiferimento.selected);