Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

293
Views
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 answers
Answer question

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 Report

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!