12

我有 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(){

});

服务.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 ?有什么帮助吗?

4

3 回答 3

25

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

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

$scope.services.push.apply($scope.services, data.services);
于 2013-09-26T09:34:00.317 回答
7

您需要一次推送一项,例如

angular.forEach(data.services,function(item) {
     $scope.services.push(item);
});
于 2013-09-26T09:40:53.293 回答
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);
        }
       } });
于 2015-06-17T06:41:50.127 回答