2013-09-26 126 views
12

Tôi có 2 tệp json, services.json và services_show.json.At tải trang đang tìm nạp dữ liệu từ services.json và nó hoạt động đúng cách. Trên một lần nhấp nút, tôi cần tìm nạp nội dung từ service_show.json và nối thêm vào mảng dịch vụ nhưng nó không hoạt động.Thêm dữ liệu json vào mảng hiện có

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%" 
      }, 

    ] 
} 

Làm thế nào tôi có thể đẩy dữ liệu services_show.json đến $ scope.services? Bất kỳ trợ giúp nào?

+0

Bạn có ý gì khi nó không hoạt động? Bạn đang thiếu để đóng bộ điều khiển của bạn để mã sẽ không hoạt động không có vấn đề gì. Nhìn vào đây để biết mã chính xác: https://gist.github.com/VictorBjelkholm/8b2004289ed9b4336034 –

+0

Đã thử chạy mã của bạn cục bộ và nó hoạt động tốt cho tôi .. – user700284

+0

Cảm ơn tất cả vì đã trả lời .. – Gopesh

Trả lời

25

Array.prototype.push.apply() có thể được sử dụng để hợp nhất hai mảng.

Merge mảng thứ hai vào đầu một

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

Bạn cần phải đẩy một mục tại một thời điểm, giống như

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

Bạn cũng có thể sử dụng concat: Ở đây tôi có mã của riêng tôi: Vui lòng tham khảo

$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); 
     } 
     } });