Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

212
Vistas
Checks for defined data in component

Let's say I have config-items for my component. If they are available they should be in the output - otherwise not.

cfg = { name: "sam", address: "NY", age: 51 };
...
<p *ngIf="cfg.name != undefined">{{cfg.name}}</p>
<p *ngIf="cfg.address != undefined">{{cfg.address}}</p>
<p *ngIf="cfg.age != undefined">{{cfg.age}}</p>

The check for defined is pretty annoying if there are a lot of items.

Is there a way to do it better?

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

You can use the safe navigation operator (also known as optional chaining)

<p>{{cfg?.name}}</p>
<p>{{cfg?.address}}</p>
<p>{{cfg?.age}}</p>

Another option is to create a wrapping ng-container and apply the *ngIf on it.

<ng-container *ngIf="cfg">
  <p>{{cfg.name}}</p>
  <p>{{cfg.address}}</p>
  <p>{{cfg.age}}</p>
</ng-container>
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda