akz*_*z92 7 ionic2 ngfor angular
我开始使用来自Angular 1的Angular 2,我无法想象如何在对象的属性上使用ngFor.
this.myVar根据HTTP请求的结果定义,该请求定义attr属性.
这是我的代码:
# 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>
Run Code Online (Sandbox Code Playgroud)
这是错误:
EXCEPTION:TypeError:无法读取[myVar.attr in ...中的undefined属性'attr'
谢谢!
Pan*_*kar 23
因为最初直到ajax完成myVar它没有任何值并且在评估表达式时ngFor,它试图访问attr未定义对象的属性.使用[ Elvis operator] [1],?让它检查是否定义了对象,如果已定义,则只检查它的attr属性.
<div *ngFor="let item of myVar?.attr"></div>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
7478 次 |
| 最近记录: |