在订阅上没有初始值的RxJS BehaviorSubject

时间:2019-11-26 13:52:43

标签: angular rxjs

我有一个Angular应用程序,在其中我使用RxJS BehaviorSubject订阅表示“正在进行”状态的布尔值。

但是我只对状态更改何时感兴趣,而不对订阅的当前状态感兴趣。

export class ProgressService {
  private InProgress$ = new BehaviorSubject<boolean>(false);

  constructor() {}

  public getInProgressStateSubject() {
    return this.InProgress$;
  }
}

...

this.progressService.getInProgressSubject().subscribe((inProgress: boolean) => {

  // This will be triggered in the moment of subscription as well as on state chages

  if (inProgress) {
    // Toggle on state
  } else {
    // Toggle off state
  }
});

我喜欢它的工作方式,只是不希望它在订阅时触发。

RxJS中是否还有其他类似的运算符可以帮助我,或者我可以通过其他任何方式来实现?

谢谢!

1 个答案:

答案 0 :(得分:1)

我认为有几种选择:

  • 使用 <select name="post-status" id="post-status"> <option {{#getValue posts.postStatus}} value="Draft" {{/getValue}} >Draft</option> <option {{#getValue posts.postStatus}} value="private" {{/getValue}} >private</option> <option {{#getValue posts.postStatus}} value="publish" {{/getValue}} >publish</option> </select>
  • 保留BehaviorSubject,但与Subject配合使用以忽略第一个值