2

Please give me a solution for that question. I want to pass a custom object using service.

private  messageSource = new Subject<Add>();
changeMessage(message:Add) {
    console.log(message);
    this.messageSource.next(message)
    console.log(this.messageSource);
    //console.log(this.currentMessage);
}
getMessage():Observable<Add>{
    return this.messageSource.asObservable();
}

class Add {
    success:boolean;
    status:string;
    id:string;
    imgArray:string[];
}
cyberpirate92
  • 3,076
  • 4
  • 28
  • 46

1 Answers1

1

You can create a subject inside your service

messageSource: Subject<string>;

and instantiate inside your constructor

this.messageSource = new Subject<string>();

in your component you can do this,

this.yourService.messageSource.next('whatvermessage');

and if you want to subscribe to it, you can do

this.yourService.messageSource.asObservable().subscribe((value: string) => {

});
Sajeetharan
  • 216,225
  • 63
  • 350
  • 396