Index incremental function bind to a button in Angular doesn't increment the index and do not iterate through array on the button click. No matter how many times I keep clicking the button, I see the same first [0] question from array. Why is so?
component.html
<div class="card-body">
<h5 class="card-title">{{questions}}</h5>
<input *ngFor="let answer of answers"
type="radio" name="ans" [value]="answer"> {{answer}}<br>
<button class="btn btn-primary" (click)="nextQuestion()">Go somewhere</button>
</div>
component.ts
export class QuostionnaireComponent implements OnInit {
questions:any;
answers:any;
correstAnswers:any;
incremental:Observable = 0;
constructor(private questionnaire: QuestionnaireService) {
this.questions = this.questionnaire.theQuestion[this.incremental];
this.answers = this.questionnaire.theChoices[this.incremental];
}
nextQuestion():void {
this.incremental++;
}
ngOnInit() {
}
}
How can I iterate through array of questions and answers on button click correctly?
Also in my radio buttons, values work fine and I get all three answer options bind to [value] but there are no words next to a radio button, it's like my > {{answer}}<br> gets ignored. No errors, though. Thanks for the help.
[0]in your case. You may want to move your constructor code tongOnInitand then probably re-assign questions/answers inngOnChangesngOnChangesit sayscalled when any data-bound property of a directive changes. I don't use directives. Only this in my component.tsthis.questions = this.questionnaire.theQuestion[this.incremental];so it's not gonna work for me. Any ideas?nextQuestionyou would need to reassignthis.questionsandthis.answersto next set so that your component can re-render using new value.ngOnChanges, I think it serves different purpose. You should post this as an answer, I will accept it. And any idea regarding the radio buttons having no text next to them? It works binding the value, I can see in the console, but in the browser there are just 3 radio buttons next to each other.answer. Hope that clarifies.