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

1
  • For one thing getUpcomingMovies doesn't appear to be in the class... Commented Dec 15, 2016 at 22:55

1 Answer 1

2

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

<div *ngFor="let movie of movies">
Sign up to request clarification or add additional context in comments.

Comments

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.