Aditya Vashishtha
Aditya Vashishtha

Reputation: 189

passing data to another module component from other modules component which is not having child parent relation

I am having core module which having component headbar ,there headbar if admin login the name should be appear ,so what I am asking that I need to set property adminName of that header span, but i am having modal which is in another component of different module Home.module.ts .

what I want that when user clicks in login the admin name from that component should go to the headbar component ,I cant use @output property since these component not having the child=parent relation ,how can i notify change to another component I tried using component as provider in constructor function but change detection not happening.enter code here

Upvotes: 2

Views: 2220

Answers (1)

GreyBeardedGeek
GreyBeardedGeek

Reputation: 30088

The documentation at https://angular.io/guide/component-interaction has a section titled "Parent and children communicate via a service" which shows the use of an rxjs Subject.

This is good information, and will work if the two components that need to communicate exist at the same time, e.g. they have a parent-child relationship, and both are instantiated before either sends a message.

This may work for you. However, if you wind up with additional components that need the same information, and they are not part of the parent-child relationship, then you can replace the Subject in the service with a BehaviorSubject. The BehaviorSubject always returns the last item upon subscription.

Note that your service would need to be a singleton (provided from the application module) so that all components get injected with the same instance of the service.

Upvotes: 1

Related Questions