我想从调用REST端点的角度服务中返回一些数据,但是我似乎无法获得正确的数据流,因为它是异步的。无法从角度服务返回列表数据到控制器
我的服务方法。
appService.getApplicationSupportFiles = function() {
$http.post("/SEFlex/SEFlexAdmin/GetApplicationSupportFiles")
.then(function (response, status, headers, config) {
if (response.data) {
var list = JSON.parse(response.data.data);
return list;
}
});
}
我控制器
function ShowApplicationFilesController($scope, $http, $modalInstance, $log, SEApplicationService, $rootScope, AlertsService) {
$rootScope.closeAlert = AlertsService.closeAlert;
$scope.supportFiles = [];
$scope.originalSupportFiles = [];
$scope.isBusy = false;
$scope.newFile = {
localFile: "",
cloudFile: "",
}
// Load the initial files
$scope.supportFiles = SEApplicationService.getApplicationSupportFiles();
$scope.originalSupportFiles = $scope.supportFiles;
$scope.addApplicationFile = function() {
var file = { LocalPath: $scope.newFile.localFile, ShareName: "b", FolderName: "c", FileName: "d" };
$scope.supportFiles.push(file);
$scope.newFile = { localFile: "", cloudFile: "" };
}
}
的问题是填充supportFiles呼叫是异步的supportFiles是不确定的,当originalSupportFiles尝试复制的值。但是当我点击addApplicationFile()时,它也不能将它推送到值$ scope.supportFiles,因为它认为它是未定义的,所以它看起来好像根本没有加载值,即使服务被调用并返回数据。
我做错了什么?
查找到'$ q'和返回的承诺,你”我想要返回一个promise并使用.then()函数。 – CollinD