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

294
Visualizações
Angular - ngIf condition displays both the data on Initial render

In my Angular project where I get data from API and display on frontend. I am using ngIf to display data, if no data is available then elseNotDone is displayed.

The problem I am facing is when component reloads, I first see No Data available for few milliseconds and then data is displayed

How can I make sure else condition is not displayed on initial render?

<section *ngIf="data!== undefined && data.length > 0; else elseNotDone">
  <h2>Amazon Data</h2>
  <div>
  // display data from API
  </div>
</section>
  <ng-template #elseNotDone>
  No Data available
  </ng-template>
about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

This is because the component initializes before the data has arrived. Once the data arrives, the component can then use it.

I would highly recommend learning about Angular Resolvers that prevent the component from initializing before the data is available. This allows you to implement things like loaders / spinners / etc, that appear while the data is being fetched.

EDIT: From the Angular documentation: https://angular.io/api/router/Resolve

about 4 years ago · Juan Pablo Isaza Relatório

0

Unnecessarily you complicated it:

*ngIf="data !== undefined && data.length > 0; else elseNotDone"

Change it to:

*ngIf="data && data.length > 0; else elseNotDone"

For your main problem: Create a variable isLoaded and wrap your code in it:

<div *ngIf="isLoaded">
  .... your code...
</div>

It will solve your problem to show anything until data is loaded.

about 4 years ago · Juan Pablo Isaza Relatório

0

Wrap your code in the ng-container with the isLoaded condition and you should "true" this variable if you get the response from API

 <ng-container *ngIf="isLoaded">
    <section *ngIf="data && data.length > 0; else elseNotDone">
      <h2>Amazon Data</h2>
      <div>
        // display data from API
      </div>
    </section>
    <ng-template #elseNotDone>
       No Data available
    </ng-template>
</ng-container>
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