【发布时间】:2019-09-29 05:50:09
【问题描述】:
我使用init如下
$scope.init = function () {
$scope.getAllDetails();
};
$scope.getAllDetails= function () {
$http({
method: "GET",
url: "api/endpoint"
}).then(function mySuccess(response) {
$scope.processData();
}, function myError(response) {
console.log(response.statusText);
}
);
};
$scope.processData() = function() {
//Logic to calculate var1
// var1 is then passed to $scope.getID to make call to Service1.getId(var1)
}
我的第一个函数返回一个承诺:
$scope.getID = function() {
return Service1.getId("abc").then(function(response){
// In above line. Instead of manually passing `abc` I want to
//pass it as variable var1
// The desired call should be as below.
//But var1 is having null here
//return Service1.getId(var1).then(function(response){
$scope.genewtId = response.data[0].Id;
console.log($scope.genewtId);
return response.data[0].Id;
}, function(error){
console.log(error.statusText);
throw error;
});
};
第二个函数返回一个promise并接受一个参数:
$scope.getDetails = function(id) {
var genewtID = id || $scope.genewtId;
return Service2.getDetails(genewtId).then(function(response){
$scope.data1 = response.data;
console.log($scope.data1.toString());
return response.data;
}, function(error){
console.log(error.statusText);
throw error;
});
};
然后chaining两个promise:
var promise = $scope.getId();
var promise2 = promise.then(function(id) {
return $scope.getDetails(id);
});
var promise2.then(function(data) {
console.log(data);
}).catch(function(error) {
console.log(error);
});
问题:
我面临的问题与$scope.processData() 中的var1 有关
var1 在 $scope.getID 中始终具有 null 值
var1 的值为 undefined
【问题讨论】:
标签: javascript angularjs angularjs-scope angular-promise angularjs-service