javascript - Angular2 - 当两个事件发射器发出时订阅

标签 javascript angular typescript

我的组件有以下代码:

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;
        );
}

但是我无法获取元素

如何确保 selectedObjselectedSection 设置为获取 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/

相关文章:

javascript - 需要有关 Div 标签内 Img 的帮助显示在 flex 边框上方

php - 将 JavaScript escape() 移植到 PHP

angular - 如何在 ionic 中移动幻灯片时调用函数

javascript - 如何输出 javascript 函数在网页下拉菜单中选择的次数?

javascript - 如何使用 css 或 jq 在 laravel 中创建图像 slider

angular - 异步验证器未执行

javascript - 如何将 id 绑定(bind)到 PrimeNg 菜单命令

angular - 从 Angular 4.3+ 的 302(重定向)响应中检索 header

typescript - 为第三方 JQuery 插件生成 Typescript 类型?

typescript - 仅将指定的属性分配/复制到对象 (TS/JS)