I have the following code:
summaries$: BehaviorSubject<IncidentSummary[]> = new BehaviorSubject(new Array<IncidentSummary>(0));
onSearchTextChanged(val: any) {
this.summaries$.subscribe(data => this.summaries$.next(data.filter(summary =>
summary.name.startsWith(val)
|| summary.type.startsWith(val))));
}
Basically I'm filtering the data of a BehaviorSubject based on search text. Problem is that this code causes an infinite loop. I tried unsubscrib
ing from within the subscribe that doesn't help. So how do I use the data to pass on to BehaviorSubject.next
?
Don't subscribe to get the data. You may store the full data array in an own property and just call next
with the new filtered data you calculated from the full data and the search text.