Angular代码中使用 *ngIf 条件判断时如何避免空值错误
这段代码可能存在的问题是在 *ngIf 条件中使用了 orgEntity.extraProperties 的属性,但没有对其进行空值判断,可能会导致在 orgEntity.extraProperties 为空时出现错误。
要修改这段代码,可以在 *ngIf 条件中添加对 orgEntity.extraProperties 的空值判断。例如,可以使用安全导航操作符 ?. 来判断 orgEntity.extraProperties 是否存在,如下所示:
<button type='button' class='btn btn-primary btn-sm mr-2' (click)='stationbinding(tplContent,depId)'
*ngIf='orgEntity.extraProperties?.OrgType==542'>
<i class='fa fa-pencil mr-1'></i>
绑定站段
</button>
这样做可以避免在 orgEntity.extraProperties 为空时出现错误。
原文地址: https://www.cveoy.top/t/topic/pjZp 著作权归作者所有。请勿转载和采集!