Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

is it possible to use push() to add element into observable array

I created an Observable array of type string. like below

products: Observable<string[]>;

I want to push some elements into products array. I don’t want to use subscribe methods here. it is not allowing me to use the normal push method. see below code

this.products.push("mova");

what is the alternative for the above code(push element to an array)? tell me one simple way to do it. Please don’t use any dependency injection here to perform the next() and subscribe().

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You can’t push to the property which is an Observable of string[] because it is expected that it will have a stream of data which will set the values in future.

What you can do is:

products: Observable<string[]>;
items: string[] = [];

// push the values in it.

this.products = of(items); // <--it will set the observables of string[]

Still you need to subscribe to the products observables to get the data.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading