监视角度属性的变化

时间:2018-05-28 05:48:01

标签: angular

  import { Component ,OnInit} from '@angular/core';
  import { QuizService } from './quiz.service';
  import { timer } from 'rxjs'; 
  import { take, map } from 'rxjs/operators';

  @Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
  })
  export class AppComponent implements OnInit  {
  questions : any;

检查此项

  countdown; 
   count=30;
  selectedQuestion;

显示当前选定的问题

 counter=0;

跟踪得分

 score= 0;
 selectedvalue;
 length;

测验服务获得所有问题

constructor(private quiz: QuizService ){
 }
ngOnInit(){
 this.quiz.getQuestions().subscribe(
 (res) =>{
 this.questions = res;
 this.length= this.questions.length;

 if(!this.selectedQuestion) {
  this.selectedQuestion = this.questions[0];
 }

  }
 )
  this.countdown = timer(0,1000).pipe(
      take(this.count),
      map(()=> --this.count)
   );
 }

收集所选答案的价值

  onchange(e){
   this.selectedvalue =e.target.value;

   }

30秒后获得另一个问题的下一个功能

   next(){


 if(this.selectedvalue === this.selectedQuestion.Answer) {
  this.score = this.score +10;

 }
   if(this.counter < this.questions.length -1) {
   this.selectedQuestion = this.questions[this.counter + 1];
    this.counter++;
   }else{
    this.counter++;
   }


    }




      }

当coundown点击0我想执行下一个功能并再次打开计时器我怎么能实现呢

1 个答案:

答案 0 :(得分:0)

使用setter:

private _countdown;

get countdown() { return this.countdown; }
set countdown(value) {
  this.countdown = value;
  if (this.countdown === 0) { this.next(); /* And turn the timer on, however you do it */ }
}