2013-09-26 65 views
12

我有2個json文件,services.json和services_show.json.At頁面加載正在從services.json獲取數據,它正常工作。在按鈕單擊時,我需要獲取內容從service_show.json並附加到服務數組,但它不起作用。將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?任何幫助?

+0

你的意思是什麼不起作用?你錯過了關閉你的控制器,這樣代碼無論如何都不會工作。在這裏找到正確的代碼:https://gist.github.com/VictorBjelkholm/8b2004289ed9b4336034 –

+0

試過在本地運行你的代碼,它對我來說工作正常.. – user700284

+0

感謝所有的答覆.. – Gopesh

回答

7

您需要一次推一個項目,像

angular.forEach(data.services,function(item) { 
    $scope.services.push(item); 
}); 
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); 
     } 
     } });