1

I can't show an array of objects inside a template. This is part of my code:

Component.ts

export class UpcomingMoviesComponent implements OnInit {
    movies: Movie[];
}
getUpcomingMovies() {
    this.moviesRes = res.results; 
    let i = 0;
    for (let item in this.moviesRes) {
        for (let subItem in this.moviesRes[item]) {
            this.movies.push(new Movie());
            this.movies[i].id = this.moviesRes[item]["id"];
            i++;
        }
     }
});

Template.html (I have tried several things but none of them worked)

<div *ngFor="let movie of this.movies">
   {{movie.id}}
   {{movie[0].id}}
</div>

If I use the following

{{this.movies | json}}

I get this:

[
  {
    "id":346672
  }
]

Could you help me to show this object inside a template. thanks

Gust van de Wal
  • 5,211
  • 1
  • 24
  • 48
David
  • 169
  • 5
  • 14

1 Answers1

2

You shouldn't use this in templates. Compiler resolves the context, so you can just write:

<div *ngFor="let movie of movies">
Sasxa
  • 40,334
  • 16
  • 88
  • 102