我的组件有以下代码:
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;
}
所以,现在我只想在设置 selectedObj
和 selectedSection
时调用服务,但它不仅仅是简单的:
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;
);
}
但是我无法获取元素
。
如何确保 selectedObj
和 selectedSection
设置为获取 items
?
最佳答案
Observable.combineLatest(this.objects.selectedObject, this.sections.selectecSection)
.subscribe(([obj, section]) => this.myService.getItems(obj, section))
您甚至不需要本地处理程序来保留该值,只需直接获取序列即可。
关于javascript - Angular2 - 当两个事件发射器发出时订阅,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40266967/