mattc19
mattc19

Reputation: 718

How do I create an observable that fires on changes made to an array

I have an angular app where I want to have a service that pulls down a couple MB's of data from a rest service as the app initializes. I then want to update that array through other components in the app. Is there a way I can create an observable from the array and have it fire the newly updated array to the subscriptions whenever the array changes. Does anyone know if this is possible?

I was thinking of using a behaviorSubject but I'm not sure if that's the right way to do it.

Upvotes: 1

Views: 519

Answers (1)

DeborahK
DeborahK

Reputation: 60518

Yes, you can use a BehaviorSubject to send out change notifications. But depending on what you are doing with the data, you may not have to.

If you have a property in your service that holds that data and the components are binding to that data, then Angular's change detection will automatically notify the components when the data is changed.

I have a simple example here: https://blogs.msmvps.com/deborahk/build-a-simple-angular-service-to-share-data/

Upvotes: 2

Related Questions