首页 > 解决方案 > chaining methods inside *ngIf

问题描述

project[col.field][selectedUserRole.value].join(',').length

If I use the above line of code inside *ngIf or inside curly braces {{}}, I am getting

ERROR TypeError: Cannot read property 'join' of undefined

error

标签: javascriptangular

解决方案


像这样使用安全导航运算符(?)-

project[col?.field][selectedUserRole?.value]?.join(',')?.length

?或安全导航运算符在模板内绑定时检查 Null、false、未定义值。


推荐阅读