我有一个关于判断对象中是否存在某个字段的问题.
我想打印用户拥有的所有类别,所以我会这样做:
<ul *ngIf="user.categories.length > 0" *ngFor="#category of user.categories">
<li>
{{category.name}}
</li>
</ul>
原因是什么?所有数据都打印了PROPERLY份,但我在web控制台中遇到了如下错误:
Cannot read property 'name' of null
但当我做一些类似的事情时:
<ul *ngIf="user.categories.length > 0" *ngFor="#category of user.categories">
<li *ngIf="category">
{{category.name}}
</li>
</ul>
那么一切都好了.
我是不是做错了什么,或者我可能每次都要判断这个?你有没有遇到过这样的问题?