【问题标题】:How to split JSON Object in Angular 4如何在 Angular 4 中拆分 JSON 对象
【发布时间】:2018-02-23 18:24:00
【问题描述】:

我正在使用带有 Angular 4 前端的 JSON API,现在我正在尝试显示此 JSON Object 的数据。

我使用了以下代码:

<div *ngFor="let Questionnaire of struc.data">
    <span>{{Questionnaire.attributes.content.headline}}</span><br>
    <span>{{Questionnaire.attributes.content.text}}</span><br>
    <span>{{Questionnaire.attributes.content.question}}</span>
</div>

<div *ngFor="let Questionnaire of struc.data">
  <li>{{Questionnaire.attributes.content.answers}}</li>
</div>

它有效,但我的答案格式如下:

  • überhaupt nicht,Sehr selten,einige Male,häufig,sehr häufig

而不是像这样:

  • überhaupt nicht
  • 塞尔顿
  • 英格男
  • häufig
  • sehr häufig

我的代码有什么问题?

【问题讨论】:

    标签: javascript json angular api


    【解决方案1】:

    你可以这样做Questionnaire.attributes.content.answer.split(',')

    <div *ngFor="let Questionnaire of struc.data">
     <div *ngFor="let ans of Questionnaire.attributes.content.answers.split(',')">
         <li>{{ans}}</li>
      </div>
    </div>
    

    EDIT

    在您的 JSON 对象发布后,答案似乎是一个数组,您需要这样做,

    <div *ngFor="let Questionnaire of struc.data">
         <div *ngFor="let ans of Questionnaire.attributes.content.answers">
             <li>{{ans}}</li>
          </div>
      </div>
    

    DEMO

    【讨论】:

    • 我做到了,但我得到了这个错误:无法读取未定义的属性'split'。我认为这是因为我正在使用一个数组而不是字符串,因为我的 JSON 对象,我只是不'不知道怎么改
    猜你喜欢
    • 1970-01-01
    • 2017-11-11
    • 1970-01-01
    • 1970-01-01
    • 2014-10-03
    • 2017-11-12
    • 1970-01-01
    • 1970-01-01
    • 2016-06-02
    相关资源
    最近更新 更多