I'm using different module so it can be used in lazy loaded modules. here is my service file.
import {NgModule, Injectable } from '@angular/core'
import {Subject} from 'rxjs/Subject';
@Injectable()
export class ChangeEventService {
public invokeEvent:Subject<any> = new Subject();
// localId:any = { name:'', eventId:0, orgId:0};
constructor() {
}
public callComponent(currentId:any):void {
console.log("Invoked");
this.invokeEvent.next({localId:currentId});
}
}
@NgModule({
imports: [ ],
exports : [
],
providers: [ ChangeEventService ],
})
export class ChangeEventModule { }
subscription to Rxjs subject is only changing in the component/service which changes It.
export class xyz {
constructor(private change: ChangeEventService) {
this.change.invokeEvent.subscribe((value) => {
this.event = value.localId;
console.log("I'm chnaged...!!");
});
}
anyFunction(eventdata:any) {
this.change.callComponent(eventdata);
}
}
I get the updated value only In the component which change the value of invokeEvent.