Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

615
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda