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

标签 javascript json angularjs angularjs-ng-repeat

在客户端,我有一个从 REST 服务某处接收到的 json 对象。该对象具有多个属性,其中之一是字符串数组。我需要一些关于如何使用嵌入在 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>

关于javascript - 如何打印由其他属性组成的 JSON 一部分的字符串数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25163533/

相关文章:

javascript - 当插件事件被触发时,如何从 Backbone.js 中的 View 执行模型的函数?

javascript - 使用 v-for 时的顺序

javascript - Ng-repeat 不适用于 Flask,$scope.variablename = data.result 在 $.post API 调用后超出范围

angularjs - 如何嵌入到属性中?

javascript - Bootstrap AngularJS

javascript - Mongoose 在循环内获取多个对象

javascript - 以 CommonJS View 方式导入 fullPage.js

android - 如何在 ListFragment 中使用 JSON 获取图像?

java - JSON 到 Java 的转换以及使用 DAO 的映射

c# - 如何从 SQL Server 2016 中删除 json 子行中的方括号?