Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular 2 templates -Safe navigation operator for methods

I'm using the angular 2 safe navigation operator successfully for properties, which the documentation states is for property paths, but what is the best approach for methods paths?

myObj?.myMethod().myProperty

gives the Exception:

EXCEPTION: TypeError: Cannot read property 'myProperty' of null in [{{myObj?.myMethod().myProperty}}

like image 911
eyecarver Avatar asked May 09 '16 22:05

eyecarver


1 Answers

In javascript, quote from w3schools:

A JavaScript method is a property containing a function definition.

I don't see any problems why it shouldn't work like this:

{{myObj?.myMethod()?.myProperty}} 

From Angular.io cheat sheet:

<p>Employer: {{employer?.companyName}}</p>

The safe navigation operator (?) means that the employer field is optional and if undefined, the rest of the expression should be ignored

like image 194
Abdulrahman Alsoghayer Avatar answered Nov 07 '22 07:11

Abdulrahman Alsoghayer