mxmissile
mxmissile

Reputation: 11681

Sharing Data Between Components

If I need to share data between components, for example I can inject a dataService with a field named width, inputs bound to said field on each component: [(ngModel)]="dataService.width". This works, if I type in component-1 input, the data shows up in component-2's input as I type. My question is why would I need to make width Observable and subscribe to it everywhere? It seems to be updating without making it Observable/EventEmitter.

Being new to Angular I have to be missing something.

Upvotes: 1

Views: 108

Answers (1)

Tomasz Kula
Tomasz Kula

Reputation: 16847

If your example is as simple as displaying the value or binding it to the input you can stick to the non observable solution.

But real world situations are much more complex. Let's say the value in your service changes and you need to:

  • invoke a method in your component
  • delegate to a method in a service
  • emit an @Output event
  • make an http request

These are the situations where observables can really help you out.

As always, it's about choosing the right tool for the job. Sometimes a simple data binding will be enough. Other times you will have to reach for the observable solution.

Upvotes: 2

Related Questions