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
  • 2,868
  • 2
  • 27
  • 45

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
  • 203,447
  • 57
  • 330
  • 376