1

In the first component, I have a variable x that is a result of a subscription to an observable(s). Since getting this variable's value involves a lot of swithcMap-ing and similar stuff, I don't want to repeat all the convoluted logic in the second component, where I need the variable x as well (and not only in html template). Is @Input right for this or are there any other means to achieve it?

Mandy
  • 107
  • 1
  • 2
  • 9
  • 3
    well, without knowing what you are doing, I believe this would be a good usage of Input. You have a "smart" component doing all the "calculation" to find the value, and just passing the result as input to the child "display" component – Crocsx Nov 21 '19 at 06:14

1 Answers1

1

Here's an article that suggests four alternatives, any one of which might be applicable to your use case:

Sharing Data Between Angular Components - Four Methods

Apr 30, 2017 written by Jeff Delaney

  • Parent to Child: Sharing Data via Input
  • Child to Parent: Sharing Data via ViewChild
  • Child to Parent: Sharing Data via Output() and EventEmitter
  • Unrelated Components: Sharing Data with a Service

You haven't given enough details to be sure which - if any - of these methods might satisfy your use case. For example, IS there a parent/child relationship between your two components?

Here's another example, which illustrates the "Child-to-Parent, Output/EventEmitter" scenario above:

Angular 7-Sharing variable between components dynamically

paulsm4
  • 114,292
  • 17
  • 138
  • 190