我的角度应用需要将两个不同的数据对象或类似的东西链接在一起。我无法将其写成文字。
数据::::
$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}}无法运作。但我们的想法是从匹配的注释userid获取用户名。
不确定从哪开始
答案 0 :(得分: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。