代码之家  ›  专栏  ›  技术社区  ›  Dream_Cap

为什么我的角分量法不起作用?

  •  0
  • Dream_Cap  · 技术社区  · 7 年前

    当this.correct/this.incorrect设置为null/false时,h2正确/错误消息不会隐藏自身。

    这是HTML模板中应该切换的部分,但它们不是。

      <h2 *ngIf="correct">Correct</h2>
      <h2 *ngIf="incorrect">Incorrect! The answer is {{ questions[counter].answer }}</h2>
    

    compareAnswer(userAnswer) {
    
      const currentAnswer = this.questions[this.counter].answer;
      if (currentAnswer.includes(userAnswer.answer)) {
        console.log('Correct!');
        this.correctAnswers += 1;
        this.correct = true;
      } else {
        this.incorrect = true;
      }
      //the correct/incorrect h2s show and then this method should clear for the 
      //next question
      setTimeout(this.handleNextQuestion, 2000);
    }
    

    handleNextQuestion() {
      //setting these properties should connect with the ngIf on the h2's and 
      //hide them, but its not doing that.
      this.correct = null;
      this.incorrect = null;
    
      this.counter += 1;
    }
    

    组件属性正确/不正确在组件上,如下所示:

      correct = null;
      incorrect = null;
    
    1 回复  |  直到 7 年前
        1
  •  3
  •   user184994    7 年前

    这是因为 this setTimeout(this.handleNextQuestion, 2000);

    setTimeout(() => this.handleNextQuestion(), 2000);