2016-07-08 5 views
0

私は2つのjsonを持っています。例えば、最初のJSONメフメットで2 Jsonの文書の関連付けとangularJSのネスト

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

は、2つのエントリを持っている二JSON(准ユーザID)

私がしたい...

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

によって、正しいユーザーを取ることができます:

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つをオブジェクトにすると、そこからデータを簡単に取得できます。それは単純に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}) 

1関係の配列を次に、あなたは次のようにあなたは、ng-repeat内部のフィルターを作成することができますID

<li ng-repeat="post in posts"> 
    <span>Author: {{users[post.userId]}}</span> 
</li> 
関連する問題