Wolf359
Wolf359

Reputation: 2725

Angular 6 : Observable<string[]> and isEmpty() operator

I have this code :

asList: BehaviorSubject<string[]> = new BehaviorSubject<string[]>([]);    
this.list$ = this.asList.asObservable();
this.empty$ = this.list$.pipe(isEmpty());

but in template :

{{empty$ | async}}

always prints 'false', even when asList has items in it.

What am I doing wrong ?

Edit :

import { map, isEmpty } from 'rxjs/operators';
import { Observable } from 'rxjs';

Upvotes: 2

Views: 3451

Answers (1)

Poul Kruijt
Poul Kruijt

Reputation: 71961

You misinterpreted the use of isEmpty(). This checks if the observable has any stream. If it does, it emits false, and true otherwise. A BehaviousSubject will always have a value, because you initialise it with the start stream value. IsEmpty will therefor always notify false.

To check if your array is empty you should do this:

this.empty$ = this.list$.pipe(
  map(list => list.length === 0) 
);

Upvotes: 4

Related Questions