2

I got this code in AppComponent:

getPlanetsToView(page){
  let pageToView = page*10 + 10;

  for(var i=0; i<pageToView; i++){
  this.planetsToView.push(this.planets.planets[i]);
  }

}

ngOnInit() {
   this.http.get('app/planets.json').subscribe(res => {
    this.planets = res.json();
    console.log(this.planets);
    this.getPlanetsToView(0);
  });

And I have this in template:

{{planetsToView[0].name | json}}

I have got problem: Photo

When I tried with {{planetsToView[0] | json }} it works but contain another properties also.

1 Answer 1

3

Could be this problem:

At the time your component is initialized your JSON is not yet loaded into this.planets because the http.get call is asynchronous. Can you try this in your template: (Note the ? which is added)

{{planetsToView[0]?.name | json}}

The ? is called elivs operator and is used to safe guard against undefined references in view templates. Read more here:

Sign up to request clarification or add additional context in comments.

2 Comments

When I did it I got error: main.browser.ts:10 Error: Template parse errors:(…)
I don't know where was error but when I copied it, start work. Thank you!

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.