Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/29.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angular 如何避免在CombineTest之后自动订阅观测值?_Angular_Rxjs - Fatal编程技术网

Angular 如何避免在CombineTest之后自动订阅观测值?

Angular 如何避免在CombineTest之后自动订阅观测值?,angular,rxjs,Angular,Rxjs,假设我有两个观测值 obsA => this.usersList$ obsB => a function that returns an observable 我使用的联合收割机如下: return combineLatest([this.getIdFromUser$(id), this.usersList$]).pipe( map(([user, userList]) => { ... here I call another funct

假设我有两个观测值

obsA => this.usersList$
obsB => a function that returns an observable
我使用的联合收割机如下:

   return combineLatest([this.getIdFromUser$(id), this.usersList$]).pipe(
      map(([user, userList]) => {
          ... here I call another function that will at the end, add a new user to the 
              observable list.
      })
   );
单击主题后将触发此组合测试。当更新usersList$observable时,整个链将再次被调用,即使我没有再次单击,只是因为链的一个可观察对象发生了更改


我怎样才能防止这种情况发生?我不想取消对整个事件的订阅,因为您实际上可以一直单击按钮来更改按钮的状态(切换)。

如您问题下方的评论所述,您可以选择:

return this.getIdFromUser$(id)
    .pipe(
        withLatestFrom(this.usersList$),
        map(([user, userList]) => {
          ... here I call another function that will at the end, add a new user to the observable list.
      })
    );

每次
This.getIdFromUser$(id)
发出时(我假设在提到的单击事件中发生),都会发出该消息,但只有在
This.usersList$
至少发出一次之后。它将始终包含this.usersList$

的最新值。如果不查看整个用例,很难说。如果您不想听到usersList$的更改,也许您最好使用?有趣的是,在映射之前是否会使用此withLatestForm?它可以工作。现在我意识到这段代码是怎么回事了。由于此功能也在不同的位置使用,以检查用户的状态,因此。。。正在触发的多个呼叫。