0

I'm trying to display the incorrect answers made by the user on a specific quiz. But it does not display the data in my HTML. What am I doing wrong in here?

on my html:

 <ion-slide *ngFor="let incorrect of wrongAnswers;let i=index">
  <ion-label>{{i+1}}</ion-label>
  <h5>{{incorrect.questionA}}</h5>
  <h5>{{incorrect.choice}}</h5>
  <h5>{{incorrect.answer}}</h5>
  <button ion-button (click)="error()"></button>
</ion-slide>

on my typescript:

 wrongAnswers: any[] = [];


 if (answer.correct == false) {
  this.wrongAnswers.push([{
    questionA: question.questionText,
    choice: answer.selected,
    answer: answer.answer
  }]);
}

On my console this is what is being displayed:

enter image description here

1
  • Try removing the [] that surround the objects you're pushing, i.e. .push({ ... });. Commented Sep 16, 2017 at 18:51

2 Answers 2

2

You are trying to push array to already existing array, if you really want to do that use concat instead

this.wrongAnswers.concat([{
    questionA: question.questionText,
    choice: answer.selected,
    answer: answer.answer
  }]);

if you want to push one object, remove the [] , so that object will be pushed

this.wrongAnswers.push({
    questionA: question.questionText,
    choice: answer.selected,
    answer: answer.answer
  });
Sign up to request clarification or add additional context in comments.

Comments

0

The array reference isn't changing when using Array.prototype.push, therefore the angular changedetection isn't aware of the changes.

And also you were pushing an array into you array not a simple object.

Use Array.prototype.concat to create a new reference when adding an item to the array:

this.wrongAnswers = this.wrongAnswers.concat({
  questionA: question.questionText,
  choice: answer.selected,
  answer: answer.answer
});

Comments

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.