1

I'm using a JSON API with Angular 4 frontend, now I'm trying to show the data of this JSON Object.

I used the following code:

<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>

It works, but I get my answer in the format:

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

instead of looking like this:

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

What's wrong with my code?

1 Answer 1

2

You can do this 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

After your JSON object post, seems answers is an array, you need to do this,

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

DEMO

Sign up to request clarification or add additional context in comments.

1 Comment

I did but I got this error: Cannot read property 'split' of undefined .. I think its because I'm working with an array not a String due to my JSON Object, I just don't know how to changes this

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.