A view lists entries using a *ngFor based on an Observable:
view.html
<ion-searchbar [(ngModel)]="filter" (ionInput)="filterMeds()"></ion-searchbar>
<ion-list>
<ion-item *ngFor="let medicine of getMeds$() | async">
{{medicine.name}}
</ion-item>
</ion-list>
The component just relays an observable via the getMeds$() function:
component.ts
private _refreshOnInput$: Subject<any> = new Subject();
getMeds$(){
return this.medsService.medsArray$;
}
The service itself just returns the array as an observable: Observable<Medicine[]>
Medicine is a plain object made of a few strings an numbers.
Now, my question: view.html adds a searchbar. Whenever the users modifies the search criteria, the filterMeds()
function is called:
filterMeds(){
this._refreshOnInput$.next(true);
}
This following a blog post (cmd-f the text "refresh button" to see what I'm trying to implement).
I understand my *ngFor must iterate on an observable that merges both the original this.medsService.medsArray$
and the _refreshOnInput()
after applying some filtering.
** component.ts attempt **
filteredMeds$: Observable<Medicine[]>
// Constructor
// An event is fired whenever the search input's updated
filteredMeds$ = this._refreshOnInput$.map( () =>
this.medsService.medsArray$
.map(meds =>
meds.filter(med =>
med.name.toLowerCase().indexOf(this.filter.toLowerCase()) > -1
)
)
);
this.meds$ = Observable.merge(this.medsService.medsArray$, filteredMeds$);
// Updating the getMeds$() method:
getMeds$(){
return this.medsService.medsArray$;
}
I can't figure out how to create a valid filtered stream. The same error happens when I directly serve the filteredMeds$ observable to *ngFor:
ERROR Error: Error trying to diff '[object Object]'. Only arrays and iterables are allowed