如何打印作为JSON一部分的字符串数组由其他属性组成

时间:2014-08-06 14:49:50

标签: javascript json angularjs angularjs-ng-repeat

在客户端,我有一个json对象,我从某个地方的REST服务收到。该对象具有多个属性,其中一个属性是String数组。我想要一些帮助,如何使用嵌入在html中的角度JS代码来打印这个数组。 这是我的JSON对象:

[

    "title": "Developing a System for Information Management in Disaster Relief",
    "url": "http://scholar.google.com/scholar",
    "relatedTitles": 
    [
        "Distributed robotic sensor networks",
        "Improving information access for emergency",
        "Tangible and wearable user interfaces for",
        "Non-parametric inferenc",
        "Airborne near-real-time monitoring of assembly" 
    ]
]

以下是html ng-repeat的样子。

        <tr ng-repeat="publication in publications">
        <td>{{publication.title}}</td>
        <td>{{publication.url}}</td>
        <td>{{publication.relatedTitles}} </tr>

注意:“publications”是JSON对象名称

3 个答案:

答案 0 :(得分:1)

这取决于您要如何打印它。如果你想要它和数组,例如用逗号连接,请使用这个html代码:

<tr ng-repeat="publication in publications">
    <td>{{publication.title}}</td>
    <td>{{publication.url}}</td>
    <td>{{publication.relatedTitles.join(', ')}}
</tr>

另外,如果你想要一个<span>标签,你可以做另一个ng-repeat:

<tr ng-repeat="publication in publications">
    <td>{{publication.title}}</td>
    <td>{{publication.url}}</td>
    <td>
        <span ng-repeat="relatedTitle in publication.relatedTitles">
            {{publication.relatedTitles}} 
        </span>
    </td>
</tr>

答案 1 :(得分:0)

你可以嵌套ng-repeat

所以你应该可以这样做:

<tr ng-repeat="publication in publications">
        <td>{{publication.title}}</td>
        <td>{{publication.url}}</td>
        <td>
          <table>
            <tr ng-repeat="title in publication.relatedTitles">
               <td>{{title}}</td>
            </tr>
          </table>
        </td>
</tr>

答案 2 :(得分:0)

<tr ng-repeat="publication in publications">
    <td>{{publication.title}}</td>
    <td>{{publication.url}}</td>
    <td>
      <span ng-repeat="title in publication.relatedTitles">
      {{title}}<br>
      </span>
    </td>
</tr>