How to send data from one component to another via observable and display the data in the secound html template in Angular

Hello I’m trying to send an object from 1. Component to 2. Component on a … Read more How to send data from one component to another via observable and display the data in the secound html template in Angular

Hello I’m trying to send an object from 1. Component to 2. Component on a click event. Currently I try this using a data service who has a get- and set-function:

setDataObject(obj) { 
 this.subject.next(obj) 
}
getDataObject(obj): Observable <any> {
 return this.subject.asObservable() 
}

On the 1. Component I trigger the setDataObject(obj) on a cliick event.
On the 2. Component I’m now unsure how to subscribe correctly to the Observable and could just save the object from 1.Component to another variable. But I have read that you shouldn’t use the observable that way. So how can I display the object and its properties in my HTML-Template? I already tried:

*ngIf = "observable$ | async as myObj"

but while my console.log shows the obj it doesn’t get displayed in the template.

Source: JavaSript – Stack Overflow



Leave a Reply

Your email address will not be published. Required fields are marked *