2014-10-17 33 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