2016-10-26 2 views
1

I私のコンポーネントのための次のコードを持っている:Angular2 - 2つのイベントemmittersが発する際購読

ngOnInit() { 
    this.route.params.forEach((params: Params) => { 
     this.codvis = params['id']; 
    }); 
    this.objects.selectedObject.subscribe(obj => this.selectedEventObjet(obj)); 
    this.sections.selectecSection.subscribe(section => this.selectedEventSection(section)); 
} 

function selectedEventObjet(o) { 
    this.selectedObj = o; 
} 

function selectedEventSection(s) { 
    this.selectedSection = s; 
} 

だから、今、私はselectedObjselectedSectionが設定されている場合にのみ、サービスを呼び出したいが、それだけで簡単ではありません次のようになります。

ngOnInit() { 
    this.route.params.forEach((params: Params) => { 
     this.codvis = params['id']; 
    }); 
    this.objects.selectedObject.subscribe(obj => this.selectedEventObjet(obj)); 
    this.sections.selectecSection.subscribe(section => this.selectedEventSection(section)); 
    this.myService.getItems(this.selectedObj, this.selectedSection) 
     .subscribe(
      items => this.items = items; 
     ); 
} 

しかし、itemsは取得できません。

selectedObjselectedSectionitemsになるように設定するにはどうすればよいですか?

答えて

1
Observable.combineLatest(this.objects.selectedObject, this.sections.selectecSection) 
    .subscribe(([obj, section]) => this.myService.getItems(obj, section)) 

値を保持するためにローカルハンドラが必要なく、シーケンスからまっすぐに取得する必要はありません。

関連する問題