filemonczyk
filemonczyk

Reputation: 1703

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"

Upvotes: 0

Views: 735

Answers (3)

Poul Kruijt
Poul Kruijt

Reputation: 71901

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

Upvotes: 1

user5711008
user5711008

Reputation:

It's calle the safe navigator operator https://angular.io/guide/template-syntax#safe-navigation-operator and it's main purpose it's to avoid to access null or undefined object, which is really helpful when you're dealing with async data (as stated in your example).

Upvotes: 1

Sajeetharan
Sajeetharan

Reputation: 222572

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.

Upvotes: 1

Related Questions