我有这个功能:
charCtrl.loadDataFromToMonth= function (from,to,year) {
var url = servername+'admin/dashboard/getIncidentDepartByMonthFromTo/'+from+'/'+to+'/'+year;
//alert(url);
function onSuccess(response) {
console.log("+++++getIncidentDepartByMonthFromTo SUCCESS++++++");
if (response.data.success != false) {
$scope.payloadgetIncidentDepartByMonthFromTo = response.data.data;
var getIncidentDepartByMonthFromTo= $scope.payloadgetIncidentDepartByMonthFromTo;
// alert('dddd'+JSON.stringify(loadedDataByMission));
$scope.labelsf = [];
$scope.qsd = [];
$scope.dataf = [];
getIncidentDepartByMonthFromTo.forEach(function(data) {
var monthNumber=$filter('date')(data.la_date, "MM");
$scope.labelsf.push($filter('monthName')(monthNumber));
$scope.dataf.push(data.number);
$scope.qsd.push('ff','ff','ff');
//alert($scope.labelsf );
});
//alert( $scope.dataf );
charCtrl.loadDataFromToMonthArrivee(from,to,year);
} else {
alert("failure");
}
// $scope.stopSpin('spinner-0');
};
function onError(response) {
console.log("-------getIncidentDepartByMonthFromTo FAILED-------");
//$scope.stopSpin('spinner-0');
console.log(response.data);
console.log("Inside getIncidentDepartByMonthFromTo error condition...");
};
//----MAKE AJAX REQUEST CALL to GET DATA----
ajaxServicess.getData(url,username,password, 'GET', '').then(onSuccess, onError);
};
此函数返回示例:$ scope.dataf = ['45','48','255'];
我有第二个功能,顺便说一下它的功能相同,但它从其他Rest服务获取数据:
charCtrl.loadDataFromToMonthArrivee= function (from,to,year) {
var url = servername+'admin/dashboard/getIncidentArriveeByMonthFromTo/'+from+'/'+to+'/'+year;
//alert(url);
function onSuccess(response) {
console.log("+++++getIncidentDepartByMonthFromTo SUCCESS++++++");
if (response.data.success != false) {
$scope.payloadgetIncidentArriveeByMonthFromTo = response.data.data;
var getIncidentArriveeByMonthFromTo= $scope.payloadgetIncidentArriveeByMonthFromTo;
// alert('dddd'+JSON.stringify(loadedDataByMission));
$scope.labelsf = [];
$scope.qsd = [];
$scope.dataf = [];
getIncidentArriveeByMonthFromTo.forEach(function(data) {
var monthNumber=$filter('date')(data.la_date, "MM");
$scope.labelsf.push($filter('monthName')(monthNumber));
$scope.dataf.push(data.number);
//$scope.qsd.push('ff','ff','ff');
//alert($scope.labelsf );
});
alert('aqsz'+$scope.dataf );
} else {
alert("failure");
}
// $scope.stopSpin('spinner-0');
};
function onError(response) {
console.log("-------getIncidentDepartByMonthFromTo FAILED-------");
//$scope.stopSpin('spinner-0');
console.log(response.data);
console.log("Inside getIncidentDepartByMonthFromTo error condition...");
};
//----MAKE AJAX REQUEST CALL to GET DATA----
ajaxServicess.getData(url,username,password, 'GET', '').then(onSuccess, onError);
};
此函数返回示例:$ scope.dataf = ['69','50','96'];
我的问题是:有没有办法在第一个函数中声明第二个函数并获得如下结果: 此函数返回例如:$ scope.dataf = [['45','48','255'],['69','50','96']];
答案 0 :(得分:0)
我会使用promises等待第二个调用来响应并更新函数作用域中的变量(兄弟到函数)。像这样:
var aggregate = [];
charCtrl.loadDataFromToMonth()
.then(function(resp){
return new Promise(resolve, reject){
aggregate.push(resp.data); // push the response from the first call
charCtrl.loadDataFromToMonthArrivee()
.then(function(resp) {
aggregate.push(resp.data); // push the result from the second call
resolve(resp)
})
}
})
.then(function(resp){
console.log(aggregate); // should have both results
})
或者更好的是,您可以使用Promise.all(https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/all)