【发布时间】:2016-08-13 02:23:59
【问题描述】:
我有这个代码:
return WordPress.getAllCategories()
.then(function (cats) {
var category = {};
$q.all(cats.data.map(function (cat) {
return WordPress.getLatestPostOfCategory(cat.id)
.then(function (post) {
return WordPress.getMediaById(post.data.featured_media)
.then(function (media) {
console.log('post id: ' + post.data.id);
console.log('Post title: ' + post.data.title.rendered);
category.post = {};
category.post.id = post.data.id;
category.post.title = post.data.title.rendered;
category.post.content = post.data.content.rendered;
var splitted = category.post.content.split('<p><!--more--></p>');
category.post.introAsHtml = splitted[0];
category.post.contentAsHtml = splitted[1];
category.post.thumbnail = media.data.source_url;
return category;
});
});
})).then(function (res) {
console.log(res);
});
});
为杂志主页加载每个类别的最新文章(使用带有 $http 请求的 WordPress REST api)。过程如下:
1.加载所有类别。
2.获取每个类别的最新帖子。
3.获取最新帖子的媒体。
4. 根据帖子数据构建category.post 对象,并添加来自接收到的媒体的缩略图(帖子特定)。
5.所有promise解决后,$scope.categories = categories申请查看。
问题:
使用上面的代码,我可以看到控制台日志正确搜索不同的帖子和媒体,但最后我得到一个包含类别的数组,所有帖子都是相同的。相同的标题、内容、缩略图和所有内容。
我在这里的承诺做错了什么?
附:所有WordPresss 服务功能正常工作。他们通过来自 WordPress 博客的 $http 请求接收到必要的数据后,返回一个已解决的承诺。
问候。
【问题讨论】:
-
您忘记了
return来自then回调的$q.all(…)。
标签: angularjs ionic-framework promise angular-promise