Angular 2 - What do you call a function in a component for a sibling?

I have two related components that need to be connected:

<app-controls></app-controls> <app-main></app-main>

app-controls contains buttons that should trigger events in the main component of the application. Is there a way that supports Angular 2 style?

+4
source share
2 answers

You can use a template variable to get a link to a brother. If it <app-controls>has an output, where it generates events when a button is pressed, you can:

<app-controls (buttonClicked)="main.doSomething($event)"></app-controls>
<app-main #main></app-main>

or you can pass a link to @Input() siblings;, and then call methods on it:

<app-controls [sibling]="main"></app-controls>
<app-main #main></app-main>
+5
source

Two ways:

Use a shared service with dependency injection.

@Input() und @Output():

https://angular.io/docs/ts/latest/cookbook/component-communication.html

0

Source: https://habr.com/ru/post/1651597/


All Articles