Here is my example:
<li *ngIf="request.answer.user">
<a href="" >
<span ></span>
<p>user</p>
</a>
</li>
I want to show the li only if the variables exists. In some cases, there is no answer.
CodePudding user response:
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 *ngIf="pedido?.RecursoTerceiraInstancia?.Resposta">
CodePudding user response:
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'
CodePudding user response:
The quick way
<li *ngIf="pedido.RecursoTerceiraInstancia.Resposta">
<a href="" >
<span ></span>
<p>Resposta Definitiva</p>
</a>
</li>