简体   繁体   中英

What does the angular 4 .? notation mean?

My question is as in the title: what does the sign '.?' mean?

Can't find it in the docs, so that's why I'm here.

[image]="(user$ | async)?.photo"

It is called safe navigation or elvis operator, which checks the value is present

The Angular safe navigation operator (?.) is a fluent and convenient way to guard against null and undefined values in property paths. Here it is, protecting against a view render failure if the user is null.

它被称为安全导航操作符https://angular.io/guide/template-syntax#safe-navigation-operator ,其主要目的是避免访问null或未定义的对象,这在处理异步时确实很有用数据(如您的示例中所述)。

The Safe Navigation operator ?. is used to avoid a NullPointerException . Typically when you have a reference to an object you might need to verify that it is not null before accessing methods or properties of the object. To avoid this, the safe navigation operator will simply return null instead of throwing an exception.

Easily confused with the elvis operator ?: , but that's completely different and does not exist in JavaScript/TypeScript or Angular templating

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM