角JS 1.5 - 在两个组件使用观测RXJS [英] Angular JS 1.5 - Using Observable RXJS in two components

查看:100
本文介绍了角JS 1.5 - 在两个组件使用观测RXJS的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用AngularJs 1.5。我使用的NG-路线来创建SPA。 2模板我已经使用了新的角度成分作为模板,让我们称他们为'员工'和'公司'。在每个路线有一个子组件搜索公司数据库调用它companySrch。如何子部件的工作原理是companySrch组件是在为'公司'或'员工'装在用户希望的该模板中进行搜索公司数据库。当用户选择在搜索结果中的记录,该记录是通过注册RXJS观察到的广播,如下所示:

I'm using AngularJs 1.5. I am using ng-route to create a SPA. For 2 templates I have used the new Angular component as the template let's call them 'employee' and 'company'. Within each route there is a sub component that searches the company database call it companySrch. How the sub component works is the companySrch component is loaded in either 'company' or 'employee' when the user want's to search the company database within that template. When the user selects a record in the search results, the record is broadcast through a registered RXJS observable as follows:

companySrch.getRecordFn = function(result){
            companySrch.output={id:result.cs_id, type:result.type,name:result.name, active: result.active};
            //BROADCAST RECORD SELECTED via companySrchService
            companySrch.companySrchService.recordBroadcast(companySrch.output); 
            companySrch.navService.nav.navExt = false;
}   

然后,如果用户是在雇员或公司模板,该记录是通过模板的订阅的RXJS事件传递。例如两个模板分别为员工和公司:

Then if the user is in employee or company template, that record is passed through the template's subscription to the RXJS event. Example for both templates employee and company respectively:

employee.companySrchService.subscribe(function(obj) {//SELECTRECORD IS PASSED VAR FROM BROADCAST FUNCTION
            //GET COMPANY RECORD UPON BROADCAST
            employee.submit[0].cs_name = obj.name;
            employee.submit[0].cs_type = obj.type;
            employee.submit[0].fk_cs_id = obj.id;
            //employee.getRecordFn(obj.id); 
        });

company.companySrchService.subscribe(function(obj) {//SELECTRECORD IS PASSED VAR FROM BROADCAST FUNCTION
        //GET COMPANY RECORD UPON BROADCAST
        company.getRecordFn(obj.id); 
    });

我有,当我在员工模板和companySrch记录在广播订阅事件也在假设我有$ P $公司解雇问题pviously加载的公司模板。作为这种本导致错误。我以为组件完全杀死模板切换时?有没有什么办法可以prevent的RXJS发生订阅事件如果模板不认为??

The issue I have when I am in the employee template and a companySrch record is broadcast the subscribed event also is fired in the 'company' on the assumption I have previously loaded the company template. As such this causing an error. I thought components are killed completely when templates are switched?? Is there any way I can prevent the RXJS subscribe event from occurring if the template is not in view??

推荐答案

我们可以创建一个主题,将尽快发出的指令的 $摧毁事件被触发。

We can create a Subject that will emit as soon as the directive's $destroy event is triggered.

var destroy$ = new Rx.Subject();
$scope.$on('$destroy', () => destroy$.onNext());

我们可以更新我们同意让我们停止尽快摧毁$ 发光的物品流发出一次:

And we can update the stream that we subscribe to so that we stop emitting items as soon as destroy$ emits once:

company.companySrchService
    .takeUntil(destroy$)
    .subscribe(function(obj) {
        company.getRecordFn(obj.id); 
    });

这将导致您的订阅回调停止为 $摧毁事件发生后立即开火。

This will cause your subscription callback to stop firing as soon as the $destroy event occurs.

这篇关于角JS 1.5 - 在两个组件使用观测RXJS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆