微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

ajax – 结合Web服务响应的AngularJS

我有两个网络服务:

一个人返回“文章”,如下所示:

[   
    {
        "id": "1","headline": "some text","body": "some text","authorId": "2"
    },{
        "id": "2","authorId": "1"
    }
]

一个返回像这样的“作者”,给出一个id:

{
    "id": "1","name": "Test Name","email": "[email protected]","photo": "path/to/img"
}

我想将两者结合起来,这样我就可以在文章概览列表中显示作者姓名和照片.

像这样:

[   
    {
        "id": "1","authorId": "2","author_info": {
            "id": "2","name": "Another Test Name","email": "[email protected]","photo": "path/to/img"
        }
    },"authorId": "1"
        "author_info": {
            "id": "1","photo": "path/to/img"
        }
    }
]

我有一个文章”服务来获取文章,但是在返回“文章”服务输出之前,使用来自类似“作者”服务的作者信息来丰富返回的JSON的最佳方法是什么?

factory('Authors',['$http',function($http){
    var Authors = {

        data: {},get: function(id){
            return $http.get('/api/authors/' + id + '.json')
                .success(function(data) {
                    Authors.data = data;
                })
                .error(function() {
                    return {};
                });
        }
    };

    return Authors;
}]).

factory('Articles','Authors',function($http,Authors){
    var Articles = {

        data: {},query: function(){
            return $http.get('/api/articles.json')
                .success(function(result) {
                    Articles.data = result; // How to get the author info into this JSON object???
                })
                .error(function() {
                    Articles.data = [];
                });
        }
    };
    return Articles;
}])

如果这是一个完全错误方法,请告诉我.

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐