角度计数器触发事件

时间:2018-10-11 07:49:35

标签: angular typescript events ionic3 counter

打字稿:

 countDown;
 counter = 10;
 tick = 1000;    

 this.countDown = Observable.timer(0, this.tick)
  .take(this.counter)
  .map(() => --this.counter)

在HTML中:

  <div>
    <h1>Time left</h1>
    <h2>{{countDown | async | formatTime}}</h2>
  </div>

当计数器达到0时,如何触发事件?

1 个答案:

答案 0 :(得分:1)

尝试一下

this.countDown = Observable
  .timer(0, this.tick)
  .take(10)
  .filter(value => value === 0)
  .do(() => {})

您还可以使用此功能,因为您使用过take

this.countDown = Observable
  .timer(0, this.tick)
  .take(10)
  .map(() => --this.counter)
  .finally(() => {})

编辑对于版本6:

this.countDown = timer(0, this.tick)
  .pipe(
    take(this.counter),
    map(() => --this.counter),
    finalize(() => /* your event */),
)

this.countDown = timer(0, this.tick)
  .pipe(
    take(this.counter),
    map(() => --this.counter),
    filter(value => value === 0),
    tap(() => /* your event */),
)