Angularjs 承诺-我在$q all之后得到相同的结果
我有以下代码:Angularjs 承诺-我在$q all之后得到相同的结果,angularjs,ionic-framework,promise,angular-promise,Angularjs,Ionic Framework,Promise,Angular Promise,我有以下代码: 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.getMediaByI
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);
});
});
返回WordPress.getAllCategories()
.然后(功能(猫){
var-category={};
$q.all(cat.data.map)(功能(cat){
return WordPress.getLatestPostOfCategory(cat.id)
。然后(职能(职位){
返回WordPress.getMediaById(post.data.featured\u媒体)
.然后(功能(媒体){
console.log('post-id:'+post.data.id);
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(“”);
category.post.introAsHtml=splitted[0];
category.post.contentAsHtml=拆分的[1];
category.post.缩略图=media.data.source\u url;
退货类别;
});
});
})).然后(功能(res){
控制台日志(res);
});
});
为杂志主页加载每个类别的最新文章(使用WordPress REST api和$http请求)。程序如下:
1.加载所有类别。
2.获取每个类别的最新帖子。
3.获取最新帖子的媒体。
4.根据帖子数据构建类别.post
对象,并从接收到的媒体添加缩略图(特定于帖子)。
5.解决所有承诺后,$scope.categories=categories
申请查看
问题:
通过上面的代码,我可以看到控制台日志正确地搜索不同的帖子和媒体,但最后我得到了一个包含类别的数组,所有帖子都是相同的。相同的标题、内容、缩略图和所有内容
我在这里的承诺有什么错
注意:所有wordpress
服务功能均正常工作。他们通过WordPress博客的$http请求收到必要的数据后,返回一个已解决的承诺
问候。请尝试以下方法:
return WordPress.getAllCategories()
.then(function (cats) {
$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);
var category = {}; // moved declaration here to return new instance each time
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);
});
});
返回WordPress.getAllCategories()
.然后(功能(猫){
$q.all(cat.data.map)(功能(cat){
return WordPress.getLatestPostOfCategory(cat.id)
。然后(职能(职位){
返回WordPress.getMediaById(post.data.featured\u媒体)
.然后(功能(媒体){
console.log('post-id:'+post.data.id);
log('Post title:'+Post.data.title.rendered);
var category={};//将声明移到此处以每次返回新实例
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(“”);
category.post.introAsHtml=splitted[0];
category.post.contentAsHtml=拆分的[1];
category.post.缩略图=media.data.source\u url;
退货类别;
});
});
})).然后(功能(res){
控制台日志(res);
});
});
您返回的是同一个category对象实例,我只是每次在
getMediaById
callback中创建新实例。你能解释一下你在这里做了什么吗?很管用。你能编辑并解释一下你在那里做了什么吗?我也将此作为答案。谢谢你,伙计!您每次都返回相同的category对象实例,我每次都在getMediaById
CallBackbase中声明它基本上只是在末尾块中设置新的category?非常感谢@karaxuna!您忘记了从返回然后从回调中返回$q.all(…)
。