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

How to print a string array which is part of a JSON consists of other attributes

本文关键字:一部分 其他 属性 JSON 这是 打印 字符串 数组 何打印      更新时间:2023-09-26

在客户端,我有一个json对象,我从某处的REST服务接收。这个对象有不止一个属性,其中一个是字符串数组。我想要一些关于如何使用嵌入在html中的angular 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对象的名称

这取决于你想如何打印它。如果你想让它像数组一样,例如用逗号连接,使用以下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>

可以嵌套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>
<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>