简体   繁体   English

如何在 Angular /QuizApp 中显示我的对象数组中的属性

[英]How to display a property from my array of objects in Angular /QuizApp

I'm doing a Quiz Simulator App and i want after a button click "Show Answer" to show me the answer of current Question.我正在做一个测验模拟器应用程序,我想在按钮单击“显示答案”后向我显示当前问题的答案。 I'm a bit confused now, I've created a function which console.log all the correct answer from all the questions but i don't know how to Interpolate it in my app and how to do it for every current question.我现在有点困惑,我创建了一个 function 控制台。记录所有问题的所有正确答案,但我不知道如何在我的应用程序中插入它以及如何为每个当前问题做它。

btw.顺便提一句。 sry im newbie, my first post here对不起,我是新手,我在这里的第一篇文章

This is my component html:这是我的组件 html:

`<div class="container p-5 ">
  <div class="row">
    <div class = "col-sm-8 offset-2">
      <p class = "text-center">{{currentQuestion + 1 }} of {{questions.length}} </p>
      <h3>{{questions[currentQuestion].question}}</h3>
      <div  *ngFor="let question of questions[currentQuestion].answers">
        <label appOptionBackground [correctAnswer]="question.correct"  class="form-check-label" for="answersRadio">
            <input  class="form-check-input" type="radio" name="answersRadio" (change)="onAnswer(question.correct)" [disabled]="answerSelected">
          {{ question.option }}
        </label>

      </div>
      <button class="btn btn-info btn-block" (click)="showResult()">Show Result</button>
      <div *ngIf="result">
        Correct Answers: {{correctAnswers}} |  Incorrect Answers {{incorrectAnswers}}
      </div>
        <button (click) ="showAnswer()">show answer</button>

      <div>{{correctOption}}
      </div>
</div>`  

This is my component ts:这是我的组件 ts:

import { Component, OnInit } from '@angular/core';
import { QuestionsService } from '../shared/questions.service';
import {Question} from '../shared/question';

@Component({
  selector: 'app-question-learn',
  templateUrl: './question-learn.component.html',
  styleUrls: ['./question-learn.component.css']
})
export class QuestionLearnComponent implements OnInit {
questions: Question[] = [];

currentQuestion = 0;
answerSelected = false;
correctAnswers = 0;
incorrectAnswers = 0;

correctOption = '';

result = false;

constructor(private questionService: QuestionsService) { }

  ngOnInit(): void {
    this.questions = this.questionService.getQuestions();

  }



  onAnswer(option: boolean) {
    this.answerSelected = true;
    setTimeout(() => {
      this.currentQuestion++;
      this.answerSelected = false;
    }, 2000);

    if(option) {
      this.correctAnswers++;
    } else {
      this.incorrectAnswers++;
    }

  }

  showResult() {
    this.result = true;
  }

  showAnswer() {
    for (let question of this.questions) {
        for (let answer of question.answers) {
          if (answer.correct === true ) {
              this.correctOption = answer.option;
             console.log(this.correctOption);
          }
        }
    }
  }
}

Here is an image:这是一张图片:

enter image description here在此处输入图像描述

Try this.尝试这个。

  showAnswer = () => {
    const question = this.questions[this.currentQuestion];
    for (let answer of question.answers) {
      if (answer.correct === true ) {
        this.correctOption = answer.option;
        console.log(this.correctOption);
      }
    }
  }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 角度-如何显示对象数组 - Angular - How to display array of objects 如何在 Angular 4 中显示数组的对象? - How to display objects of an array in Angular 4? 如何显示填充有对象 ionic angular 的数组中的数据 - How to display data from an array filled with objects ionic angular 如何在 Angular Typescript 中显示数组中的嵌套对象 - How to display nested objects from array in Angular Typescript 如何将 id 与对象数组映射以在视图中显示名称属性 - Angular - How to map id with an array of objects to display name property in view- Angular 角度4-从对象数组读取属性 - angular 4 - read property from an array of objects angular 7 显示来自对象数组的图像 - angular 7 display image from array of objects 如何通过使用 Angular 通过属性名称将属性名称检查到对象数组来从 object 分配值 - How to assign values from object by checking property name to array of objects via property name using Angular 从对象数组中,提取属性值作为 angular 中的数组 - From an array of objects, extract value of a property as array in angular 如何循环遍历对象数组中的对象数组并使用 Angular/Ionic 在我的 HTML 中显示? - How do I loop through an array of objects that's within an array of objects and display in my HTML using Angular/Ionic?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM