Angular 的安全导航操作符 (?.
) 是一种流畅而便利的方式,用来保护出现在属性路径中 null 和 undefined 值。
举例:The hero's name is {{Hero.name}}
若name为空时,会报错,导致整个程序崩溃。若改成如下形式:
The hero's name is {{Hero?.name}}
这样即使name为空也不会报错,只是不显示name而已。
Angular 的安全导航操作符 (?.
) 是一种流畅而便利的方式,用来保护出现在属性路径中 null 和 undefined 值。
举例:The hero's name is {{Hero.name}}
若name为空时,会报错,导致整个程序崩溃。若改成如下形式:
The hero's name is {{Hero?.name}}
这样即使name为空也不会报错,只是不显示name而已。