2014-10-17 31 views
0

我的角度應用程序需要將兩個不同的數據對象連接在一起,或類似的東西。我無法將其寫入文字。鏈接數據:Angular

數據::::

$scope.users = [{userid: "5", name: "Bobby"},{userid: "3", name: "Fett"}]; 
$scope.comments = [{id: "1", content: "a comment", userid: "3"},{id: "2", content: "another comment", userid: "3"}]; 

指令::::

<article ng-repeat="comment in comments"> 
    Posted by: {{user.name}} Comment: {{comment.content}} 
</article> 

顯然,{{user.name}}是行不通的。但想法是從匹配的評論用戶標識中獲取用戶名。

不是真的知道從哪裏開始的這

回答

0

一個可能的解決方案可能是一個用戶索引,以獲得相應的用戶:

var userIndex = {}; 
for (var i = 0, len = users.length; i < len; i++) { 
    userIndex[users[i].userid] = users[i]; 
} 

$scope.userIndex = userIndex; 
$scope.comments = comments; 

使用這個指標,你可以得到的用戶名:

<article ng-repeat="comment in comments"> 
    Posted by: {{ userIndex[comment.userid].name}} Comment: {{comment.content}} 
</article> 

看到這個plunker

+0

謝謝!這就是我一直在尋找的! – tylmcooper 2014-10-20 13:18:44