如果不檢查{object.field}是否存在,則出錯關于檢查對象中是否存在某個字段,我有一個問題。我想打印用戶擁有的所有類別,所以我正在做這樣的事情: <ul *ngIf="user.categories.length > 0" *ngFor="#category of user.categories">
<li>
{{category.name}}
</li>
</ul>原因是什么?所有的數據都是恰如其分打印,但我在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>那就沒事了。我是做錯什么了還是每次都要檢查一下?你有過這樣的問題嗎?
添加回答
舉報
0/150
提交
取消
