Reputation: 33
Here is my example:
<li class="list-group-item" *ngIf="request.answer.user">
<a href="" class="d-flex flex-column align-items-center">
<span class="icofont-law-order icofont-2x"></span>
<p>user</p>
</a>
</li>
I want to show the li only if the variables exists. In some cases, there is no answer.
Upvotes: 0
Views: 2251
Reputation: 187
The quick way
<li class="list-group-item" *ngIf="pedido.RecursoTerceiraInstancia.Resposta">
<a href="" class="d-flex flex-column align-items-center">
<span class="icofont-law-order icofont-2x"></span>
<p>Resposta Definitiva</p>
</a>
</li>
Upvotes: 0
Reputation: 4617
If you go for *ngIf="true ? something : somethingElse"
it is pretty obvious that the something
will always execute, no matter what (because true
is always evaluated to true...)
In your case, you can use the optional chaining operator to make sure the objects exist before accessing properties on them. Your ngIf
could look something like this:
<li class="list-group-item" *ngIf="pedido?.RecursoTerceiraInstancia?.Resposta">
Upvotes: 2
Reputation: 9128
In Javascript, there's a data type exactly for the purpose of checking if "variable exists": undefined
.
So you can do this:
typeof pedido.RecursoTerceiraInstancia.Resposta !== 'undefined'
Upvotes: 0