Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular Safe Navigation Operator, Dynamic Properties and Bracket Notation

Angular 2 has a 'safe navigation operator' that allows template references to potentially undefined child properties in your components.

Is there any way to use this with dynamic properties and bracket notation? e.g.,

<input [type]="text" [ngModel]="formValues?[control]">

Where control is another variable from my component telling the template which form value to use and formValues is loaded async so could potentially be null.

like image 972
Roy Reiss Avatar asked Mar 01 '17 19:03

Roy Reiss


1 Answers

I think that you can't, but this is what I'm using, simpler than ngif but empty string and 0 will not do what is after the && :

<input [type]="text" [ngModel]="formValues && formValues[control]">
like image 66
captain hak Avatar answered Nov 17 '22 13:11

captain hak