Testing Anurag
Testing Anurag

Reputation: 643

Passing object from one component to another component in Angular

I am working in Angular 7 . where I am passing object to another component , I am doing it using @input decorative , but components are siblings , so How I can pass the object to another component

Upvotes: 0

Views: 4890

Answers (2)

anees
anees

Reputation: 344

It is better use Service for the communication in this scenario.

In service u can use Subject which can act as both observer and observable.

ex.

In Service

  someSubject = new Subject<any>();

In component one

 someService.someSubject.next(objectUWantToSend);

In Component two u can subscribe or vise versa

someService.someSubject.subscribe((receiveObjectHere) => {});

Upvotes: 1

toothful
toothful

Reputation: 892

@input decorative can only be used for passing values from parent to child components.

For communication between siblings or any components use rxjs subject and Observable instead.

You can follow this - http://jasonwatmore.com/post/2018/06/25/angular-6-communicating-between-components-with-observable-subject

Upvotes: 0

Related Questions