将json数据添加到现有数组

时间:2013-09-26 09:31:23

标签: javascript json angularjs

我有2个json文件,services.json和services_show.json。页面加载我从services.json获取数据并且它正常工作。点击一下按钮,我需要从service_show.json中获取内容并追加到服务数组但它不起作用。

var beautyApp = angular.module('findbeauty', []);

beautyApp.controller('beautycntrl',function($scope,$http){

    $http.get('http://localhost/Find-Beauty/media/services.json').success(function(data) {
        $scope.services=data.services;
        $scope.services1=data.services1;
    });

    $scope.Add = function(){

        $http.get('http://localhost/Find-Beauty/media/services_show.json').success(function(data) {
            console.log(angular.toJson(data.services));
            $scope.services.push(data.services);

        });

    };

    $scope.ViewMore = function(){

});

Services.json

{
"services":[
{
            "name": "Arun",
            "gender": "Damen",
            "duration": "1.5 Stunden",
            "price": "€65,00",
            "imagepath": "media/images/prfilepic1.png",
            "percentage": "90%"
        },

    ],
    "services1":[

    {
            "name": "Schnitt & Föhnen",
            "gender": "Damen",
            "duration": "1.5 Stunden",
            "price": "€65,00",
            "imagepath": "media/images/profilepic4.png",
            "percentage": "25%"
        },


    ]
}

service_show.json

{
"services":[
{
                "name": "Schnitt & Föhnen",
                "gender": "Damen",
                "duration": "1.5 Stunden",
                "price": "€65,00",
                "imagepath": "media/images/profilepic4.png",
                "percentage": "5%"
            },

    ],
    "services1":[

    {
                "name": "Schnitt & Föhnen",
                "gender": "Damen",
                "duration": "1.5 Stunden",
                "price": "€65,00",
                "imagepath": "media/images/prfilepic1.png",
                "percentage": "50%"
            },

    ]
}

如何将services_show.json数据推送到$ scope.services?任何帮助?

3 个答案:

答案 0 :(得分:25)

Array.prototype.push.apply()可用于合并两个数组。

  

将第二个数组合并到第一个数组

$scope.services.push.apply($scope.services, data.services);

答案 1 :(得分:7)

您需要一次按下一个项目,例如

angular.forEach(data.services,function(item) {
     $scope.services.push(item);
});

答案 2 :(得分:0)

您还可以使用 concat : 我有自己的代码:请参考

$http.get('store/LoadAlbums/', {'iCategoryID': iCategoryID}).then(function (data) {
      for (var i = 0; i < $scope.result.mainalbums.length; i++) {
         if($scope.result.mainalbums[i].iCategoryID == iCategoryID){
            $scope.result.mainalbums[i].albums = $scope.result.mainalbums[i].albums.concat(data.data.albums);
            $scope.result.mainalbums[i].totalAlbumCategory = $scope.result.mainalbums[i].albums.concat(data.data.totalAlbumCategory);
            $scope.result.mainalbums[i].loadmore = $scope.result.mainalbums[i].albums.concat(data.data.loadmore);
            $scope.result.mainalbums[i].newpage = $scope.result.mainalbums[i].albums.concat(data.data.newpage);
        }
       } });