angular2可观测扫描一次,然后由多个异步

angular2可观测扫描一次,然后由多个异步,angular,rxjs,Angular,Rxjs,我需要在不同的时刻两次绑定到共享的可观察对象。第二个绑定在第一次计算时得到null,直到出现下一项 下面是组件类: 以下是模板: 显示扫描 展示其他 下一个 其他|异步:{{'+(其他|异步)} 扫描|异步:{{'+(扫描|异步)} 以下是(更新:plunker更新为接受答案) 需要使用share()。在不使用share()的情况下,所有绑定从一开始就可以工作,但随后会为每个主题调用两次scan。next()调用(在本plunker示例中,在单独的项目实例上)。出于许多原因,我希望避免重

我需要在不同的时刻两次绑定到共享的可观察对象。第二个绑定在第一次计算时得到
null
,直到出现下一项

下面是组件类:

以下是模板:


显示扫描
展示其他
下一个
其他|异步:{{'+(其他|异步)}
扫描|异步:{{'+(扫描|异步)}
以下是(更新:plunker更新为接受答案)

需要使用
share()。在不使用
share()
的情况下,所有绑定从一开始就可以工作,但随后会为每个
主题调用两次
scan
。next()
调用(在本plunker示例中,在单独的项目实例上)。出于许多原因,我希望避免重复的
扫描
调用-至少不要为每个订阅者重复完全相同的作业和相同的结果


我想知道什么是避免多个
share
(即使用其他
Observable
方法)调用的正确方法,并且在绑定新的
async
时仍然提供最后一个元素。

对,您需要一个hot Observable,以便共享子描述,而不是单独订阅。您可能对Andre的这段视频感兴趣:。您可能还对Paul Taylor 2015年的演讲感兴趣:

基本上,您可以这样重写代码:

import {Subject, ReplaySubject} from "rxjs/Rx"; // only really need these two

/* your other code */

export class App {

  // plain old subject for clicks
  // i believe you can get an event stream somewhere?
  // sorry, i don't know angular2
  subject = new Subject();

  // replays the last event for observers on subscription
  main = new ReplaySubject(1);

  // you could apply transforms here if you want
  scan = this.main
  other = this.main

  constructor() {
    // take the events that come in
    this.subject

      // start our observable with an initial event
      .startWith(0)

      // when subject emits, this will run and update
      .scan((current, change) => current + change)

      // now we can subscribe our replay subject
      .subscribe(this.main);
  }
}

希望这能有所帮助。

好吧,你需要一个热的可观察对象,这样你就可以共享一个subscription,而不是单独订阅。您可能对Andre的这段视频感兴趣:。您可能还对Paul Taylor 2015年的演讲感兴趣:

基本上,您可以这样重写代码:

import {Subject, ReplaySubject} from "rxjs/Rx"; // only really need these two

/* your other code */

export class App {

  // plain old subject for clicks
  // i believe you can get an event stream somewhere?
  // sorry, i don't know angular2
  subject = new Subject();

  // replays the last event for observers on subscription
  main = new ReplaySubject(1);

  // you could apply transforms here if you want
  scan = this.main
  other = this.main

  constructor() {
    // take the events that come in
    this.subject

      // start our observable with an initial event
      .startWith(0)

      // when subject emits, this will run and update
      .scan((current, change) => current + change)

      // now we can subscribe our replay subject
      .subscribe(this.main);
  }
}

希望这能有所帮助。

这真是个好答案。我已经更新了plunker,并在自己更复杂的案例中使用了它——正如预期的那样,它在最近的angular2测试版和RxJS 5测试版中表现良好。非常感谢。这真是个好答案。我已经更新了plunker,并在自己更复杂的案例中使用了它——正如预期的那样,它在最近的angular2测试版和RxJS 5测试版中表现良好。非常感谢。
import {Subject, ReplaySubject} from "rxjs/Rx"; // only really need these two

/* your other code */

export class App {

  // plain old subject for clicks
  // i believe you can get an event stream somewhere?
  // sorry, i don't know angular2
  subject = new Subject();

  // replays the last event for observers on subscription
  main = new ReplaySubject(1);

  // you could apply transforms here if you want
  scan = this.main
  other = this.main

  constructor() {
    // take the events that come in
    this.subject

      // start our observable with an initial event
      .startWith(0)

      // when subject emits, this will run and update
      .scan((current, change) => current + change)

      // now we can subscribe our replay subject
      .subscribe(this.main);
  }
}