Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ionic-framework/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ionic-framework/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angularjs 承诺-我在$q all之后得到相同的结果_Angularjs_Ionic Framework_Promise_Angular Promise - Fatal编程技术网

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(…)