2015-06-30 89 views
1

我有一個名爲「posts」的列表,它包含下面的對象。如何以角度綁定對象中的嵌套列表

{_id: "559301710de9c23407b237ee" 
author: {user_id: "557c6922925a18a81930d2ce", username: "test"} 
comment_id: [] 
content: "<p>123</p>" 
created_at: "2015-06-30T20:52:01.738Z" 
image_id: [] 
like_user_id: ["557c6922925a18a81930d2ce"] 
likes: 1 
tags: [{_id: "558ded1b8526b45407fd0bb1", tag_name: "1"}, {_id: "559301630de9c23407b237ec", tag_name: "2"},…] 
title: "123"} 

我想提取TAG_NAME,並顯示在HTML

<a ng-repeat="post in bests" ng-href="/post/{{post._id}}"> 
    <em class="tag">{{post.tags.tag_name}}</em> //This appearently doesn't work! 
    <span>{{post.title}}</span> 
    <em class="likes">+{{post.likes}}</em> 
    <time>{{post.created_at}}</time> 
</a> 

一切正常,除了TAG_NAME罰款。

我試過在ng-repeat中使用ng-repeat,它似乎不起作用。你怎麼做呢?

回答

3

嘗試

<em class="tag" ng-repeat="tag in post.tags">{{tag.tag_name}}</em> 

post.tags是一個數組,因此顯示tag_names的最簡單方法是使用ng-repeat ...

3

tags處於陣列因此,你需要一個指數,以使用它

{{post.tags[0].tag_name}} 

<em class="tag">{{post.tags[0].tag_name}}</em> //<== sample code that works 
          |  

       Your desired index goes here 

另外,您可以使用迭代ng-repeat

3

此作品

<em class="tag" ng-repeat="tag in post.tags">{{tag.tag_name}}</em>