gpt4 book ai didi

javascript - 如何从 rxjs Observable 获取刻度数

转载 作者:行者123 更新时间:2023-11-29 21:12:59 25 4
gpt4 key购买 nike

如何获取这个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.. 次,并且每次都在模板中显示插值

如果我这样做,它会返回百分比变量为 NaN

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

最佳答案

然后您可以创建一个变量,该变量将负责存储 observable 运行的次数。然后对该值应用 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);
}

关于javascript - 如何从 rxjs Observable 获取刻度数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40815665/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com