2

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.

1 Answer 1

2

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>

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for the feedback. I'm switching a lot between TS and Dart recently and get sometimes confused about what works where.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.