0

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.

3 Answers 3

2

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">
Sign up to request clarification or add additional context in comments.

Comments

0

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'

Comments

0

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>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.