I am trying to use observables, and I get an error saying "Cannot read property 'unsubscribe' of null". I am wondering where I should call my unsubscribe method. I am also wondering if my observable setup here is wrong. I have a service, a component, and a HTML template that looks something like this :
// *** Service ***
private elements : Map<Category, string[]> = new Map<Category, string[]>();
// I want to populate the map when the service is constructed
constructor(private httpClient : HttpClient){
for (enum in Category){
if (!isNaN(enum as any)){
this.httpClient.get<string[]>(someUrl + enum).subscibe(data => {
this.elements.set(category, data);
});
}
}
}
getElements(category: Category) : Observable<string[]> {
return Observable.create(this.elements.get(category));
}
.
.
.
// *** The component ***
getCategoryList(category : Category) : Observable<string[]> {
this.categoryService.getElements(category);
}
.
.
.
// *** The template ****
<div *ngFor="let element of getCategoryList('WARM_ELEMENTS') | async ">
<div class="row">
{{element}}
</div>
</div>
I guess I need an unsubscribe method somewhere, or / and there is something wrong with my observable setup here (I'm learning Angular now).