2014-11-21 3 views
1

AngularJS를 RESTAPI와 함께 사용하여 서버에서 JSON 형식으로 고객 데이터를 전송할 수 있습니다. 아래 내 각도 코드 :AngularJS 테이블에 JSON 데이터를 표시합니다.

{ 
"data": [ 
    { 
     "id": "1", 
     "artist": "Gotye75", 
     "title": "Making Mirrors7" 
    }, 
    { 
     "id": "100", 
     "artist": "ytttt5", 
     "title": "1231" 
    }, 
    { 
     "id": "101", 
     "artist": "65", 
     "title": "565444555" 
    }, 
    { 
     "id": "102", 
     "artist": "6", 
     "title": "6" 
    }, 
    { 
     "id": "103", 
     "artist": "y", 
     "title": "yy" 
    }, 
    { 
     "id": "104", 
     "artist": "ty", 
     "title": "yt" 
    }, 
    { 
     "id": "109", 
     "artist": "ytrer", 
     "title": "yt" 
    } 
    ] 
} 

내 JSON은 "데이터"를 포함하지 않는 경우 테이블에서 JSON 데이터를 표시 할 수 있어요 듣고 :

app.factory("services", ['$http', function($http) { 
    var serviceBase = '/api/album'; 
    var obj = {}; 
    obj.getCustomers = function(){ 
     return $http.get(serviceBase); 
    }; 
    return obj; 
}]); 
    app.controller('listCtrl', function ($scope, services) { 
services.getCustomers().then(function(data){ 
    alert(JSON.stringify(data.data)); 
    $scope.customers = data.data; 
    }); 
}); 

여기 내 JSON 데이터입니다. 그러나 내 jSON 데이터가 "데이터"헤더와 함께 제공되면이를 표시 할 수 없습니다. 나는 묻고 있는데, Angular의 솔루션은 JSON 객체를 파싱하는 것이다. 예를 들어

이 BackboneJS에있는 경우, 나는 간단하게 할 수있는

parse: function (response) { 
    //alert(JSON.stringify(response.data)); 
    return response.data; 
} 

내가 그것을 각도에서 어떻게 할 수 있습니까?

답변

1

내가

하여 해결
app.controller('listCtrl', function ($scope, services) { 
    services.getCustomers().then(function(data){ 
    //notice that i added the third data 
    $scope.customers = data.data.data; 
    }); 
}); 
+0

잘 했어 내 친구 – Pravin

3

$http는 응답 객체와의 약속을 해결합니다. 데이터는 응답 객체의 data 속성을 통해 액세스됩니다. 그래서 배열에 도착, 당신은 ...

$scope.customers = data.data.data; 

$http의 약속이 응답 객체를 펼쳤다 .success() 편리한 방법으로 강화된다 사용해야 할 것

services.getCustomers().success(function(data){ 
    alert(JSON.stringify(data.data)); 
    $scope.customers = data.data; 
}); 
+0

나는 고마워! – max

+0

DataService.getDataUsingPromises() .then (function (data) { $ scope.netspendovertime_data = data; }); 컨트롤러의 내 약속에 예외/오류 처리를 추가하는 방법은 무엇입니까? 그리고 내 서비스에서 나는 반환 $ q.when (originalRequest) . 다음 (기능 (res) { 데이터 = res.ResultSet.Response; 반환 데이터, }); –

+2

문제를 보여주는 바이올린을 게시 할 수 있습니까? –

관련 문제