链接RxJs观测量的角2

链接RxJs观测量的角2

问题描述:

我有我的角2的应用程序一个打字稿函数返回一个可观察到,推动网络API数据传回给消费者,这样的事情:链接RxJs观测量的角2

public getApiData(): Observable { 
    let readySource = Observable.from('no', 'no', 'ready'); 
    let dataSource = http.get('api/getData'); 
    // ... magic here 
    return chainedObservable; 
} 

然而,相当比通常返回http.get Observable,我需要链接此HTTP调用到另一个readySource Observable,它指示API是否准备好调用(它实际上检查背景数据同步作业是否已完成)。

如何将这两个观察值链接在一起,因此只有在readySource推入特定值时才会调用HTTP调用,例如, “准备”?

(请注意,香草flatMap /没有的SelectMany不太符合这里的要求,因为我需要等待,直到第一个可观测调用推前一秒一个特定的值。)

我会混合filter操作flatMap之一。下面的示例介绍了如何当用户填写一个特定的值(“准备好”在这里)触发请求:

@Component({ 
    selector: 'my-app', 
    template: ` 
    <input [ngFormControl]="ctrl"/> 
    <button (click)="onClick()">Click</button> 
    ` 
}) 
export class AppComponent { 
    constructor(private http:Http) { 
    this.ctrl = new Control(); 
    let readySource = this.ctrl.valueChanges.filter((val) => val === 'ready'); 
    readySource.flatMap(() => http.get('data.json')).subscribe(() => { 
     console.log('test'); 
    }); 
    } 
} 

看到这个plunkr:https://plnkr.co/edit/yZL1wRw7GYhUkkhnL9S0?p=preview