在angularjs中$ http的2个json数据之间的链接

时间:2017-10-12 01:50:13

标签: javascript angularjs json

我有2个http获取JSON格式数据的第一个:

category:2
city_code:"C!000871"
country:"TH"
hotel_code:"H!0532874"
name:"Chaulty Towers Guest House"

然后依赖于hotel_code我发送了另一个$http.get请求,新数据如下:

 address:"149/41 Moo 4, Maret, Lamai 84310 Koh Samui   Moo 4, Maret, Lamai"
 category:"2"
 city_code:"C!000871"
 code:"H!0532874"

hotel_codecode相同,我可以在与address

匹配的第二个数据的hotel_code视图中显示

PS: 在我看来,我对第一组数据ng-repeat="data in data | limitTo: 10"

2 个答案:

答案 0 :(得分:2)

您可以尝试:添加评论:

// Iterate 1st Array
for (var i = 0; i < HouseNames.length; i++) {
    // Iterate 2nd Array
    for (var k = 0; k < HotelDetails.length; k++) {
        // Check for Hotel code
        if (HouseNames[i].hotel_code == HotelDetails[k].code) {
            // If hotel code matches add it to 1st array
            HouseNames[i].hotelDetails = HotelDetails[k];
        }
    }
}
console.log(HouseNames);

答案 1 :(得分:1)

您可以创建一些函数来从控制器获取第二个数据集,然后使用相同数组的ng-repeat制作地图并显示数据。

$scope.data = [your data here].map(function(item){
    get_2nd_json_resp_by_hotel_code(item.hotel_code).then(
        function(res){
            $scope.data = angular.extend($scope.data ,res);
        }
    );
});