Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does ?. mean in angular 5? [duplicate]

Tags:

angular

I know the if I have something like product.id == 1 ? stuff : not stuff. That means if id = 1, then choose "stuff". If not, then choose "not stuff". What does the following mean:

product?.id.name
like image 259
RiceRiceBaby Avatar asked Jul 31 '18 14:07

RiceRiceBaby


1 Answers

? Means safe navigation operator

From Docs

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 currentHero is null.

This specifically means that if the value you are binding to the view is null then it should return null else the actual value, so that you dont get any issues while rendering the template.

In the above sample code you provided ,

product?.id.name

it checks whether produce object exists and then it will check if there is an id. since you dont have ? after id. it will throw an error "cannot read property of 'name' undefined".

like image 92
Sajeetharan Avatar answered Sep 21 '22 20:09

Sajeetharan