无限循环订阅事件RxJS

时间:2019-10-07 13:53:50

标签: angular rxjs observable reactive-programming

我遇到了一个问题: 当用户单击按钮-触发事件(变化率项目)时。 但是我对此事件有不必要的循环(无限循环)。该活动已订阅,我认为有问题。

我的ngOnInit

    private click$ = new Subject<any>();

    ngOnInit() {

       this.subscription.add(this.movieUserRateService.searchRates({ movieId: [this.movieId] }).subscribe());

       this.click$
        .pipe(switchMap((rate) => this.movieUserRateService.getCalculateRate(this.movieId)))
         .subscribe(rate => {
           this.rateValue = rate;
           this.setRate();
         });

   }

onChange函数

onChange(event) {
    const rateObj = { value: event.value, movieId: this.movieId };
    this.click$.next(rateObj);
  }

setRate函数

  setRate() {
    console.log('setRate');
    this.myRating.setValue(this.rateValue);
  }

我尝试了其他方式:

 onChange(event) {

    const rateObj = { value: event.value, movieId: this.movieId };
    const subscription = this.movieUserRateService.saveRate(rateObj)
          .pipe(
            switchMap((rate) => this.movieUserRateService.getCalculateRate(this.movieId))
          )
          .subscribe(rate => {
            this.rateValue = rate;
            this.setRate();
            subscription.unsubscribe()
          });

}

如果我不使用 this.setRate(); -无限循环消失。但是我需要使用 setRate (用于监视更改等级)。

我在做什么错了?

0 个答案:

没有答案
相关问题