I want to create service, which can interact with one component. All another components in my app, should be able to call this service, and this service should interact with
as this post is a bit old, I actualize the response of Tudor the stackblitz
the service
private customSubject = new Subject();
customObservable = this.customSubject.asObservable();
// Service message commands
callComponentMethod(value:any) {
this.customSubject.next(value);
}
the main-component
constructor(private communicationService:CommunicationService){}
ngOnInit()
{
this.communicationService.customObservable.subscribe((res) => {
this.myFunction(res)
}
);
}
myFunction(res:any)
{
alert(res)
}
Another component that call to the method of the service
constructor( private communicationService: CommunicationService ) { }
click() {
this.communicationService.callComponentMethod("hello word");
}