2 回答

TA貢獻1831條經驗 獲得超4個贊
聽起來這可能是閉包的問題。函數可以在定義的位置(而不是在調用的位置)訪問代碼的包含閉包。
var1是真正的null,還是事實上的undefined?如果為undefined,則可能需要做的是使getId以var1作為參數,而不是依賴于從包含的閉包中提取引用(由于上述原因,該方法不起作用)。如果是true null,那么問題可能出在計算var1的代碼中,您已經省略了它。
編輯:該getID()函數看起來非常相似,但它會像這樣開始:
$scope.getID = function(var1) {
代替這個:
$scope.getID = function(var1) {
調用它,您可能會執行以下操作:
var var1 = somethingToGetTheValueOfVar1();
$scope.getID(var1);

TA貢獻1820條經驗 獲得超10個贊
我現在面臨的問題是關系到var1中$scope.processData()的var1總是具有null值范圍內$scope.getID的var1值undefined
var1作為參數添加到函數定義中:
?$?s?c?o?p?e?.?g?e?t?I?D? ?=? ?f?u?n?c?t?i?o?n?(?)? ?{?
$scope.getID = function(var1) {
//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;
});
};
我正在嘗試按以下方式調用該函數,但無法正常工作
$scope.processData() = function() { ... $scope.getID(var1); }
賦值語句的左側必須是變量或屬性訪問器。
$?s?c?o?p?e?.?p?r?o?c?e?s?s?D?a?t?a?(?)? ?=? ?f?u?n?c?t?i?o?n?(?)? ?{? ?
$scope.processData = function(data) {
//...
var var1 //= something
return $scope.getID(var1);
}
然后確保正確鏈接:
$scope.getAllDetails= function () {
return $http({
method: "GET",
url: "api/endpoint"
}).then(function mySuccess(response) {
var data = response.data;
return $scope.processData(data);
}, function myError(response) {
console.log(response.statusText);
//IMPORTANT RE-THROW error
throw response;
});
};
將值返回到.then塊很重要。否則,新的承諾將解決為undefined。
同樣重要的是要從蓋帽上扔出去.catch。否則,新的承諾將從拒絕的承諾轉換為已實現的承諾。
添加回答
舉報