Javascript 一个服务调用的AngularJs输出取决于另一个服务调用
我是新加入angularjs的,目前我面临一个问题,就像- 我正在呼叫一个服务,那就是-Javascript 一个服务调用的AngularJs输出取决于另一个服务调用,javascript,jquery,angularjs,html,Javascript,Jquery,Angularjs,Html,我是新加入angularjs的,目前我面临一个问题,就像- 我正在呼叫一个服务,那就是- if($scope.tab === "1"){ $scope.loadreports(); $scope.loaddocumentForHighlighting(); } $scope.loadreports = function(){ uploadService.loadReports(uploadServi
if($scope.tab === "1"){
$scope.loadreports();
$scope.loaddocumentForHighlighting();
}
$scope.loadreports = function(){
uploadService.loadReports(uploadService.currentFileName, $scope.documentType, jdCurrentFileName)
.then(function (response) { uploadservice.setreprtdata(response) },function (error) {
$scope.loadingReports = false;
$scope.errorMessage = error.status + " : " + error.statusText;
if (error.status === 401) {
loginService.authenticationError();
}
}
Now ,
$scope.loaddocumentforhighlighting = function(){
uploadService.getDocumentAsHTML($scope.documentType, uploadService.currentFileName)
.then(function (data) {}, .finally(function ( console.log(uploadservice.getreportdata()) ) {
$scope.showDocumentReloadPanel = false;
});
}
现在,当第一个调用是
loadreports
时会发生什么,然后需要一些时间才能从服务器获得响应。但是在这个方法中,我有一个集合,我想在第二个方法中使用。但在此之前,第二个方法将被执行。所以,进入该方法会给我未定义的。但问题是我也得到了第一种方法的响应。但现在不行。那么,我如何解决这个问题呢?这些方法应该会回报承诺:
$scope.loadreports = function(){
return uploadService.loadReports(...).then(...);
}
$scope.loaddocumentforhighlighting = function(){
return uploadService.getDocumentAsHTML(...).then(...);
}
然后像这样被锁住:
$scope.loadreports().then(function () {
return $scope.loaddocumentForHighlighting()
});
您使用的措辞可能存在重复,这有点难以理解,但我认为您需要的是承诺,它是使用$q以angular实现的。请提供更完整的代码和解释,我将删除我的反对票。@RandyCasburn我相信答案已经非常具体。我添加了所有相关的代码修复。我同意你的看法,因为我每天都这么做——不是每个人都有你的知识深度。