簡體   English   中英

如何打印作為JSON一部分的字符串數組包含其他屬性

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

在客戶端,我有一個從REST服務接收的json對象。 該對象具有多個屬性,其中一個是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對象名稱

這取決於您要如何打印它。 如果您希望它像和數組一樣,例如以逗號連接,請使用以下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>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM