AngularJS 承诺已解决但无法访问数据
AngularJS Promise Resolved but cannot reach data
var promise = GetQuestions.quote.get();
console.log(promise);
promise.$promise.then(function(quote) {
$scope.quotes = quote;
console.log($scope.quotes);
for( quote in $scope.quotes){
console.log(quote.cite);
}
});
工厂
.factory('GetQuestions', ['$resource', function($resource){
return {
quote: $resource('**Resource Link**', {}, { get: { method: 'GET', isArray: true }})
};
}])
这是console.log(承诺):
[$promise: Promise, $resolved: false]
这里是console.log($scope.quotes):
并且console.log(quote.cite)记录未定义
问题:
我的问题是我无法访问 "cites",承诺已解决,但我似乎无法正确访问数据。我试过做 promise.then()(而不是 promise.$promise.then()),但是我得到一个错误告诉我 promise.then() 不是一个函数。我不确定这是为什么。有帮助吗?
而不是 for var in list
语法,尝试
$scope.quotes.forEach(function(quote) {
console.log(quote.cite);
});
for var in list
不应该用于遍历数组。它的预期目的是迭代对象属性。当您使用它来遍历数组时,您经常会遇到奇怪的行为。
见this SO question。
var promise = GetQuestions.quote.get();
console.log(promise);
promise.$promise.then(function(quote) {
$scope.quotes = quote;
console.log($scope.quotes);
for( quote in $scope.quotes){
console.log(quote.cite);
}
});
工厂
.factory('GetQuestions', ['$resource', function($resource){
return {
quote: $resource('**Resource Link**', {}, { get: { method: 'GET', isArray: true }})
};
}])
这是console.log(承诺):
[$promise: Promise, $resolved: false]
这里是console.log($scope.quotes):
并且console.log(quote.cite)记录未定义
问题:
我的问题是我无法访问 "cites",承诺已解决,但我似乎无法正确访问数据。我试过做 promise.then()(而不是 promise.$promise.then()),但是我得到一个错误告诉我 promise.then() 不是一个函数。我不确定这是为什么。有帮助吗?
而不是 for var in list
语法,尝试
$scope.quotes.forEach(function(quote) {
console.log(quote.cite);
});
for var in list
不应该用于遍历数组。它的预期目的是迭代对象属性。当您使用它来遍历数组时,您经常会遇到奇怪的行为。
见this SO question。