如何从 rxjs Observable 获取刻度数

How to get number of ticks from rxjs Observable

如何得到这个observable的次数运行

   this.clock = Observable.interval(1000).map(function(value){
     if(value == 0){
       return  value * 100 / 60;
     }
     return value * 100 / 60;
    }).take(61);

我想得到这个 Observable 运行 1 或 2 或 3.. 次,并每次在模板中插值显示

如果我这样做,return 百分比变量将为 NaN

this.clock.subscribe(function(x){
              console.log(x);
              console.log("percentage " + this.percentage);
              this.percentage = this.percentage + 1;
    })

然后你可以创建一个变量来负责存储可观察的次数运行。然后对该值应用 async 以显示 observable 返回的值。

{{ clockCount | async }}

代码

clockCount: any;
count: number = 0;
ngOnInit(){
    this.clock = Observable.interval(1000).map((value) => {
       if(value == 0){
         return  value * 100 / 60;
       }
       return value * 100 / 60;
    }).take(61);
    this.clockCount = this.clock.do(() => this.count = ++this.count);
}