matth3o
matth3o

Reputation: 3659

dart angular2 - access map value component in template

I'd like to know if there is a way to write something like :

 <span>{{myObject?.myField['myKey']}}</span>

in my template.

Thanks for your help.

Upvotes: 2

Views: 210

Answers (1)

G&#252;nter Z&#246;chbauer
G&#252;nter Z&#246;chbauer

Reputation: 657048

The problem with your code is that ['myKey'] is evaluated even when myObject is null. This would require ?[] but that is not supported

<span>{{myObject?.myField != null ? myObject.myField['myKey'] : null}}</span>

or

<span *ngIf="myObject?.myField != null">{{myObject.myField['myKey']}}</span>

Maybe this works as well (don't remember)

<span *ngIf="myObject?.myField">{{myObject.myField['myKey']}}</span>

Upvotes: 2

Related Questions