Reputation: 3659
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
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