Victor Callegari
Victor Callegari

Reputation: 71

Angular 2 - @Input and @Output

I am not sure if I should use @Input and @Output​ since I think these decorators only work when you wan to communicate a component parent with a child one or vice versa. Can you please clarify it or correct me?

I have 3 components that are in the same level and I am looking for setting a value to a variable from component A in component B. They do no have any relationship.

Thanks

Upvotes: 1

Views: 756

Answers (1)

Eeks33
Eeks33

Reputation: 2315

In short, you are right, Input() and Output() only communicate with the parent component. Your parent component could however use the Output() of component B and pass it to the Input() of component A. Using the angular life cycle hook ngOnChanges would aid with that.

There are also many ways to achieve Component A modifying a value in Component B. You could use subjects, services, parent components, as well as many other strategies. This might get into the question of how you want to manage state in your application. https://bertrandg.github.io/angular2-application-state-management-strategy/

At scale, you definitely want to be consistent with state management in your app. ngrx (https://github.com/ngrx) provides a great redux-like strategy

Upvotes: 1

Related Questions