首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >ngx-倒计时(已完成)事件未触发

ngx-倒计时(已完成)事件未触发
EN

Stack Overflow用户
提问于 2019-09-22 22:15:09
回答 2查看 2K关注 0票数 0

我正在尝试添加一个倒计时计时器到我的应用程序中,并找到了一个用于此目的的ngx-countdown组件,但是(finished)事件并未被触发。我做错了什么?

我的组件:

代码语言:javascript
运行
复制
import { Component, OnInit, ViewChild } from '@angular/core';
import { Question } from '../../models/Question'
import { AnswerService } from 'src/app/services/answer.service';
import { ActivatedRoute } from '@angular/router';
import { CountdownComponent } from 'ngx-countdown';

@Component({
  selector: 'app-answer-sheet',
  templateUrl: './answer-sheet.component.html',
  styleUrls: ['./answer-sheet.component.css']
})
export class AnswerSheetComponent implements OnInit {

  questions: Question[];
  @ViewChild('countdown', { static: false }) private countdown: CountdownComponent;

  constructor(private route: ActivatedRoute, private answerService: AnswerService) { }

  ngOnInit() {
    let id = this.route.snapshot.paramMap.get('id')
    this.answerService.getQuestions(id).subscribe(questions => this.questions = questions);
  }

  ngAfterViewInit () {
    this.countdown.pause();
    this.countdown.resume();
  }

  answerQuestion(question: Question) {
    var q = this.questions.find(q => q.nr === question.nr)
    q.userAnswer = question.userAnswer
  }

  handleEvent($event) {
    if ($event.left === 0) {
      alert("Time's up, mate")
    }
  }

  onTimerFinished() {
    alert("Time's up, mate")
  }

和标记:

代码语言:javascript
运行
复制
<p class="countdown">
  <countdown #countdown [config]="{leftTime: 5}" (finished)="onTimerFinished()" >$!m!:$!s!</countdown>
</p>
<form (ngSubmit)="onAnswerSubmit()">
  <app-question 
    *ngFor="let question of questions"
    [question]="question"
    (answerQuestion)="answerQuestion($event)"
    >
  </app-question>
  <input mat-raised-button type="submit" value="Submit">
</form>

我使用的是Angular版本8。

EN

回答 2

Stack Overflow用户

发布于 2020-03-16 19:22:17

在html中尝试:

代码语言:javascript
运行
复制
  <countdown #countdown [config]="{leftTime: 5} (event)="onTimerFinished($event)" >$!m!:$!s!</countdown>

在ts中:

代码语言:javascript
运行
复制
 onTimerFinished(e:Event){
   if (e["action"] == "done"){
      //your code here
    }
  }
票数 2
EN

Stack Overflow用户

发布于 2020-11-10 12:07:52

我也有同样的问题,直到我在官方网站上搜索了他们的官方文档,在这里https://github.com/cipchk/ngx-countdown他们提到了类型和它已经安装在模块中

它为我解决了

代码语言:javascript
运行
复制
  onTimerFinished(e: CountdownEvent) {
    if (e.action == 'done') {
 
    }
  }
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/58050041

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档