2016-07-08 4 views
0

두 개의 json이 있습니다. 예를 들어 첫 번째 JSON의 메 흐멧에서 2 Json 문서가 연관되어 있고 angularJS에 중첩되어 있습니다.

[ {"id":"1", "userID":"23"} , {"id":"2", "userID":"23"}, {"id":"3", "userID":"22"}] 

1

[ {"id":"23", "name":"mehmet"} , {"id":"22", "name":"jack"} ] 

2

은 (동료 아이디) 내가 원하는

에 ... 2 개 항목 초 JSON을

mehmet (source first json) 
    id:1 (second json) 
    id:2 (second json) 

jack(source first json) 
    id:3 (second json) 

내 제어 ler :

.controller('ListviewCtrl', function($scope, $http, SERVER) { 

    $scope.topics = []; 
    function loadTopics(params, callback){ 
     $http.get(SERVER.url+'/listtopic', {params: params}) 
     .success(function(response){ 
     var topics = []; 

     angular.forEach(response, function(result){ 


      topics.push(result); 
     }); 

     callback(topics); 
     }); 
    }; 



     $scope.tweetler = []; 
    function loadTweetler(params, callback){ 
     $http.get(SERVER.url+'/tweet') 
     .success(function(response){ 
     var tweetler = []; 

     angular.forEach(response, function(result){ 
      tweetler.push(result); 
     }); 
     callback(tweetler); 
     }); 
    }; 

ng-repeat 및 list associate 2 json을 사용하는 방법은 무엇입니까?

답변

0

당신은 아이ng-repeat 내부 필터를 만들 수 있습니다 :

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

 
app.controller('mainCtrl', function($scope) { 
 
    $scope.parents = [ 
 
    { 
 
     "id":23, 
 
     "name":"mehmet" 
 
    }, 
 
    { 
 
     "id":22, 
 
     "name":"jack" 
 
    } 
 
    ]; 
 

 
    $scope.childs = [ 
 
    { 
 
     "id":1, 
 
     "userID":23 
 
    }, 
 
    { 
 
     "id":2, 
 
     "userID":23 
 
    }, 
 
    { 
 
     "id":3, 
 
     "userID":22 
 
    } 
 
    ]; 
 
});
<!DOCTYPE html> 
 
<html ng-app="app"> 
 

 
<head> 
 
    <script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.5.7/angular.min.js"></script> 
 
</head> 
 

 
<body ng-controller="mainCtrl"> 
 
    <ul> 
 
    <li ng-repeat-start="parent in parents" ng-bind="parent.name"></li> 
 
    <ul ng-repeat-end ng-repeat="child in childs | filter: { userID: parent.id }"> 
 
     <li ng-bind="'Id:' + child.id"></li> 
 
    </ul> 
 
    </ul> 
 
</body> 
 

 
</html>

0

해당 배열 중 하나를 객체로 만든 다음 해당 배열에서 데이터를 쉽게 가져올 수 있습니다. 그것은 간단 1 걸릴하려면 다음과 같이

var users = [ 
    {"id":"1", "name":"mehmet"}, 
    {"id":"2", "name":"jack"} 
] 

// This is simple 1:1 relationship.. ID:NAME 
{ 
    "1": "mehmet", 
    "2": "jack" 
} 

// To do it, is very simple 
$scope.users = {}; 
angular.forEach(users, function(user) { $scope.users[user.id] = user.name}) 

는 그런 다음 ID로 올바른 사용자를 취할 수 있습니다 .. (1) 관계의 배열은

<li ng-repeat="post in posts"> 
    <span>Author: {{users[post.userId]}}</span> 
</li> 
관련 문제