我需要通过我的 angularjs 服务发出一个 http 请求。我知道它是异步工作的,但我无法捕获我的数据。
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
}
我正在调用 getDataRifList 函数。我希望有一个包含一些数据的列表。
这就像它被调用了两次。第一次列表为空,第二次有数据,但我的函数已经执行。
错误是什么?
编辑
该调用有效,但是当我需要设置 filtroDataRiferimento 选定属性时,dataRifValues[0] 为空。相反,values 属性具有值。
var filtroDataRiferimento = {
label : "Data di riferimento",
name : "dataRiferimento",
type : "single",
values : dataRifValues,
selected : dataRifValues[0],
flagMatch : false
};
$scope.filtraMonitoraggioProcessi(filtroDataRiferimento.selected);