1

Component:

export class HomeComponent implements OnInit {

  tridentData: TridentData;

  constructor(private tridentServices: TridentService) { }

  ngOnInit() {
    this.tridentServices.getTridentData()
                    .subscribe(
                      (data: TridentData) => {
                        this.tridentData = data;
                      }
                    )
  }
}

HTML:

<h1>{{ tridentData }}</h1>   <---results in undefined

The Problem:

I am calling getTridentData() (A GET request, that returns json) on my tridentData object. I am having a ASYNC problem where the view loads before my tridentData object can retrieve the data the db. This results tridentData in undefined and my whole page breaks. How do I implement a loading animation and have the view wait for the data to load before popping in?

2

3 Answers 3

4

On your template you can add a condition to the h1 tag :

<h1 *ngIf="tridentData">{{ tridentData }}</h1>
Sign up to request clarification or add additional context in comments.

Comments

3

We'd need to see your getTridentData() code to be sure but really what you probably want to do is use the async pipe.

{{ tridentData | async }}

Comments

0
      showTridentData:Promise<boolean>;

      ngOnInit() {
        this.tridentServices.getTridentData()
                        .subscribe(
                          (data: TridentData) => {
                            this.tridentData = data;
                            this.showTridentData=Promise.resolve(true);
                          }
                        );
      }

in your HTML use as this

 <h1 *ngIf="showTridentData | async">{{tridentData}}</h1>

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.