我需要使用$http.jsonp
获取Feed,然后返回deferred.resolve(feed)
,feed
值应传递给链式函数EntryStateUrlService.getEntryStateUrl
。
出于某种原因,它将FeedService.parseFeed(feedSrc)
评估为undefined
,因此无法在then()
上调用undefined
方法。如何解决?
app.service('LoadData', ['FeedService', 'EntryStateUrlService', '$q', function(FeedService, EntryStateUrlService, $q) {
var deferred = $q.defer();
this.loadData = function(feedSrc) {
FeedService.parseFeed(feedSrc).then(EntryStateUrlService.getEntryStateUrl).then(function(urls) {
$rootScope.links = urls;
});
}
}]); //TypeError: undefined is not a function
app.service('FeedService', function($http, $q) {
var deferred = $q.defer();
this.parseFeed = function(url) {
$http.jsonp('//ajax.googleapis.com/ajax/services/feed/load?v=1.0&callback=JSON_CALLBACK&q=' + encodeURIComponent(url))
.success(function(res) {
deferred.resolve(res.data.responseData.feed.entries); //TypeError: Cannot read property 'responseData' of undefined
}).error(function() {
deferred.reject();
});
return deferred.promise(); //TypeError: object is not a function
}
});
app.service('EntryStateUrlService', ['$state', '$q', function($state, $q) {
var deferred = $q.defer();
this.getEntryStateUrl = function(feeds) {
var idx = 0;
feeds.forEach(function(e) {
$http.jsonp(e.link).success(function(data) {
/*stuff*/
deferred.resolve('root.' + generatedStateName);
});
}); //forEach
} //getEntryStateUrl
return deferred.promise();
}]); //EntryStateUrlService
更新
将return deferred.promise();
添加到EntryStateUrlService
和FeedService
后,我TypeError: object is not a function
return deferred.promise();
的{{1}} {}}。
答案 0 :(得分:1)
在FeedService中返回deferred.promise,同样在EntryStateUrlService中,调用forEach中的多个服务,因此如果在完成所有服务后需要解析promise,请使用$ q.all,如下所示。
app.service('LoadData', ['FeedService', 'EntryStateUrlService', '$q', function(FeedService, EntryStateUrlService, $q) {
this.loadData = function(feedSrc) {
FeedService.parseFeed(feedSrc).then(EntryStateUrlService.getEntryStateUrl).then(function(urls) {
$rootScope.links = urls;
}, function(failureException) {
// failure error handler comes here.
});
}
}]); //TypeError: undefined is not a function
app.service('FeedService', function($http, $q) {
this.parseFeed = function(url) {
var deferred = $q.defer();
$http.jsonp('//ajax.googleapis.com/ajax/services/feed/load?v=1.0&callback=JSON_CALLBACK&q=' + encodeURIComponent(url))
.success(function(res) {
deferred.resolve(res.data.responseData.feed.entries); //TypeError: Cannot read property 'responseData' of undefined
}).error(function() {
deferred.reject();
});
return deferred.promise;
}
});
app.service('EntryStateUrlService', ['$state', '$q', function($state, $q) {
this.getEntryStateUrl = function(feeds) {
var deferred = $q.defer();
var idx = 0, promises = [];
feeds.forEach(function(e) {
$http.jsonp(e.link).success(function(data) {
/*stuff*/
});
}); //forEach
// once all the jsonp service in the for loop is finished, then resolve the promise
$q.all(promises).then(function() {
deferred.resolve('root.' + generatedStateName);
}, deferred.reject);
return deferred.promise;
} //getEntryStateUrl
}]); //EntryStateUrlService
答案 1 :(得分:0)
你parseFeed
函数不应该返回延迟的吗?
this.parseFeed = function(url) {
$http.jsonp('//ajax.googleapis.com/ajax/services/feed/load?v=1.0&callback=JSON_CALLBACK&q=' + encodeURIComponent(url))
.success(function(res) {
deferred.resolve(res.data.responseData.feed.entries);
}).error(function() {
deferred.reject();
});
return deferred;
}
答案 2 :(得分:0)
我错过了你的代码中的承诺。我认为您必须在FeedService中返回一个承诺:return deferred.promise();
而不是return deferred;