Angular 使用角度从10数到1

Angular 使用角度从10数到1,angular,count,Angular,Count,我正在处理angular项目,在我下面的代码中是从1到10的计数循环,但是我想让计数循环从10到1 我的代码: TOTAL = 10; // total number of images currentIndex = 1; private interval; play() { this.interval = setInterval(() => { console.log(this.currentIndex) if (this.currentIndex +

我正在处理angular项目,在我下面的代码中是从1到10的计数循环,但是我想让计数循环从10到1

我的代码:

 TOTAL = 10; // total number of images
  currentIndex = 1;
  private interval;

play() {

  this.interval = setInterval(() => {
    console.log(this.currentIndex)

    if (this.currentIndex + 1 > this.TOTAL) this.currentIndex =1;
    else this.currentIndex++
  }, 600)

  this.presentToast()
}

stop() {
  if (this.interval) {
    clearInterval(this.interval);
    console.log(this.currentIndex);
  }
html



تشغيل ايقاف
有什么想法吗

您可以使用rxjs中的timer()函数,该函数在间隔1秒后由以下代码发出

timer(0,1000)
如果要反向,则需要通过映射将计数器减少1 当值为0时,要完成计数器,我们使用take(x),它获取前x个值并完成


实例:

您可以使用另一个变量进行循环,该变量与当前索引相反。然后将此变量用于html,如下所示:

<img src="index={{reversedIndex}}&false=True&lat=33&lon=44" name="animation"
      class="img-fluid center-block" />
      <br>

      <ion-button (click)="play()">تشغيل</ion-button>
      <ion-button (click)="stop()">ايقاف</ion-button>


تشغيل ايقاف

工作示例:

谢谢您的回答。但我想要数字循环请澄清你所说的数字循环是什么意思我的项目是动画卫星气象图像我想通过从10到1循环数字从10到1,即使我使用像这样的当前索引数字,但为什么他从10到0计数?我们可以从10到1吗?可以。只需将条件更改为if(this.currentIndex+2>this.TOTAL)this.currentIndex=1;我更新了上面链接中的代码
<img src="index={{reversedIndex}}&false=True&lat=33&lon=44" name="animation"
      class="img-fluid center-block" />
      <br>

      <ion-button (click)="play()">تشغيل</ion-button>
      <ion-button (click)="stop()">ايقاف</ion-button>