Roy Reiss
Roy Reiss

Reputation: 993

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.

Upvotes: 12

Views: 1810

Answers (1)

captain hak
captain hak

Reputation: 912

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]">

Upvotes: 1

Related Questions