Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

614
Views
how to check if object is null or undefined in angular in ngIF condition

I have this inside my code:

 <div *ngIf="(listCount$| async) > 0 ">

but it won't pass the pipeline because it says

Object is possibly 'null' or 'undefined'.

in ts file:

readonly listCount$ = new BehaviorSubject<number | undefined>(undefined);

any help, can i do this in one line?

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

It's an async operation that might result in an undefined value that's why you are getting the error. Provide a default value in case of undefined in your template condition.

<div *ngIf="((listCount$| async) ?? 0) > 0 ">

OR set the strictNullChecks to false in your tsconfig.json file.

"strictNullChecks": false

Demo:- https://stackblitz.com/edit/angular-ivy-kheete?file=src%2Fapp%2Fapp.component.html

about 4 years ago · Juan Pablo Isaza Report

0

You can use the as syntax for a clean syntax which will allow you to use the listCount as a variable. this variable will evaluate as a condition for a truty/falsy argument so (0, undefined, null, '', NaN) will evaluate as false.

<div *ngIf="(listCount$ | async) as listCount;">hello</div>
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!