我正在使用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;
});
};
然后链接两个诺言:
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);
});
问题:我面对有关的问题var1中$scope.processData() 的var1总是具有null中值$scope.getID 的var1值undefined
慕容708150
拉莫斯之舞
相关分类