Lucas_Santos
Lucas_Santos

Reputation: 4740

Inform to another component that a button was clicked in a specific component

How can I inform to my home-day-component that my user clicked on a button that are inside my header-component ? Both component's are inside the home-component

For example, if my user Click in the button Previous Month, the home-day-component need to know it.

Home Component

<header-component></header-component>
<div>
   <home-day-component></home-day-component>
</div>

Header Component

<nav class="nav" id="calendar">
   <button                                            
      (click)="previousMonth()">
   </button>
   {{current.month}} {{current.year}}
   <button                    
      (click)="nextMonth()">                    
   </button>
</nav>

Upvotes: 0

Views: 358

Answers (1)

Sajeetharan
Sajeetharan

Reputation: 222522

You should use a service with Subject,

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) => {

});

Upvotes: 1

Related Questions