Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

angular – how to write correct ngIf condition – is array length is bigger then 1

I have angular code and I put condition like this:

<li *ngIf="carProfiles$.length > 1">

and this works, but this .length is red in my editor and it writes

unresolved variable length

I’m worried about this because no matter this code works it seems I did something wrong. In the ts file:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

readonly carProfiles$: Observable<Profile[]>;

and inside constructor:

this.carProfiles$ = this.allProfiles$.pipe(map((profiles) => profiles.filter((p) => ProfileHelper.isCar(p))));

Any help? DO I need to worry about why text .length is red and did I write condition in the correct way?

>Solution :

You are not trying to check an array length. You are trying to check the observable’s length (and is not correct) which emits an array. Instead you can first extract the emitting array inside observable by using async pipe, then check for length. This async pipe is subscribing to this Observable in the backgorund and unsubscribing from it.

<li *ngIf="(carProfiles$| async)?.length > 0">
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading