I develop a web application with tech Angular. But now I am having a problem, how can I pass or set a property from a compoennt to the other component, which are not parent-child component.
Detials: I have created a compoent called Serach, There is a button in this component, if this button is clicked, will call some REST APIs and will wait to get some data from backend. By calling this REST CALL, will in the other compoennt called Home show 'LOADING....'.
the component Search and the component HOME ist not parent-child component. and the Search compoent is in the Header Component, and Home Component will be shown, if the web app loaded. That means, that these 2 Components are be shown in the same time.
I have tried to use Message Service to transport a Message e.g. 'LOADING'.
<mat-sidenav-content>
<div class="app-header">
<app-header></app-header>
</div>
<div class="mat-sidenav-content">
<main class="content">
<app-breadcrumb></app-breadcrumb>
<router-outlet></router-outlet>
</main>
</div>
</mat-sidenav-content>
Search component is located in app header component.
the function in serach component is as follows:
public search(): void {
this.messageService.sendMessage("LOADING");
this.loadData.loadData(this.key).subscribe(() => {
this.router.navigate(['overview']);
}
, (error) => {
this.openDialog();
}
);
}
the code in home.ts
export class HomeComponent implements OnInit {
public message = [];
subscription: Subscription;
constructor(private messageService: MessageService,
private router: Router) {
this.subscription = this.messageService.getMessage().subscribe((message) => {
this.message.push(message);
});
}
ngOnInit() {
}
}
and the HTML code:
<div *ngIf="message.includes('LOADING')">
<app-loading></app-loading>
</div>
someone has any idea??