I am trying to implement a callback function in a service class, which has to return data back to the component class.
ChatComponent.ts
export class ChatComponent implements OnInit {
constructor( public _chatService : ChatService) {
_chatService.joinChat()
}
OnInit(){
}
// I need to get the `msg` object from the ChatService class
didReceiveMessage(msg){
console.log(“Message received from chat service class”+msg);
}
}
ChatService.ts
import { Component, Input } from '@angular/core';
import { Injectable } from '@angular/core';
@Injectable()
export class ChatService {
public chatObj : SomeChatObject;
constructor() { }
joinChat(){
//Join chat related functionality
this.chatObj.addHandler(this.onMessageReceivedHandler, null, "message");
}
onMessageReceivedHandler = (message) => {
//Send `message` back to `didReceiveMessage ` method in ChatComponent.ts
return true;
}
}
I’ve seen an example of using Http Observable callback. Here I’ve added my callback explicitly using addHandler. I will get the message object in the ‘onMessageReceivedHandler’ method. But i need to pass it to ChatComponent. How can I pass the data.