* ngFor object undefined attribute

I start with Angular 2, starting with Angular 1, and I cannot figure out how to use ngFor for an attribute of an object. this.myVardetermined by the result of the HTTP request that defines the attribute attr.

Here is my code:

# my_page.ts
import {Page} from 'ionic-angular';
import {MyService} from '../../services/my_service';

@Page({
  templateUrl: 'build/pages/my_page/my_page.html',
  providers: [MyService]
})


export class MyPage {
  public myVar;

  constructor(private myService: MyService) {
    this.myService.fetch().subscribe(
      data => this.myVar = data
    );
  }
}

# my_page.html
<div *ngFor="#item of myVar.attr">
</div>

And this is a mistake:

EXCEPTION: TypeError: cannot read attr property from undefined in [myVar.attr in ...]

Thank!

+4
source share
1 answer

ajax myVar ngFor attr undefined. [Elvis operator] [1], ?, , , , attr.

<div *ngFor="let item of myVar?.attr"></div>
+11

All Articles