user2929318 user2929318 - 1 month ago 8x
HTTP Question

Nested object array in objects - Angular 2

I'm using Angular 2 and JSONP to get the value from the api. The api is returning an Object (Nested Objects). Below is the code which I'm trying to use.




return this.jsonp.get('').map(
(res) => res.json()


export class MovieComponent implements OnInit {

movies = Object;
constructor(private dataService: DataService) { }

ngOnInit() {
(data) => this.movies = data

HTML Template

{{movies.budget}} //This is working
{{movies.genres[0].id}} //This is not working

In the HTML Template, I've tried using Elvis Operator, but that is not returning any value. The console log is showing proper data as one single object. How to get the id and name of different genres?


is not working, even after providing an index with it.


It is working with the knowledge of the index! See my plunker as a demo:

But the index has to be there, so check it first with *ngIf or use *ngFor to iterate through all possible items.

import {Component, NgModule} from '@angular/core'
import {BrowserModule} from '@angular/platform-browser'

  selector: 'my-app',
  template: `
      <h2 *ngIf="names && names.length">Hello {{names[0]}}</h2>

      <h2 *ngFor="let n of names">Hi {{n}}</h2>
export class App {
  constructor() {
    this.names = [];

    setTimeout(() => this.names = ['Angular2', 'Name2', 'name3'], 3000);

  imports: [ BrowserModule ],
  declarations: [ App ],
  bootstrap: [ App ]
export class AppModule {}