MisterniceGuy
MisterniceGuy

Reputation: 1786

How can i assign a empty Array to a Observable in Angular

I have dropdown Boxes in my forms which use via a custom component and i populate them with data by pasing the lookup value via a GUID to the component. All works as expected and i get my data etc. But now i need to be able to also clear the data in dropdown based on user selection. Lets say user selects in first dropdown fruits, based on that i want to provide in the selection all fruits which i have no problem doing. But now user goes back and changes its selection from fruits to soda, which means the previous selected apple is no longer valid and needs to be cleared. So i am looking for a simple way to clear all values from a dropdown box.

to get my data into dropdown i call

socialOptions$: Observable<Array<IServerDropdownOption>>;
ngOnInit() { 
    this.socialOptions$ = this.guidService.fetchAll(this.guids.SOCIAL_LABELS).pipe(shareReplay());
  }

when i try this.countryOptions$ = [] i get this error enter image description here

Upvotes: 0

Views: 2608

Answers (1)

Gagan
Gagan

Reputation: 315

If all you want is to get an empty array from your Observable, then the following should do

import { of } from 'rxjs';
...
this.countryOptions$ = of([]);

Explanation

In your question you are trying to do the following this.countryOptions$ = [] which is incorrect because you are trying to assign an array to type Observable which are not compatible. The Rxjs of operator creates an Observable from any provided array. Read more about the of operator here.. https://www.learnrxjs.io/learn-rxjs/operators/creation/of

Upvotes: 7

Related Questions