1

If I try to access an element's property of an array like this:

{{myArray[0].myProperty}}

I get 'Runtime Error... Uncaught (in promise): TypeError: Cannot read property 'property' of undefined'

But if I use *ngFor I can access every property of each element in the array. I'm missing something? How can I access a property of an specific element?

3
  • Are you getting array asynchronously? Commented Apr 27, 2017 at 4:13
  • 1
    Can you please include the *ngFor implementation Commented Apr 27, 2017 at 4:15
  • Yes asynchronously, but if I use the *ngFor, it works Commented Apr 27, 2017 at 4:23

2 Answers 2

2

Try using safe navigation operator. I suspect that first time your array is empty

{{ myArray[0]?.myProperty }}
Sign up to request clarification or add additional context in comments.

Comments

0

Try this:

<YOUR_TAG *ngIf="myArray">{{myArray[0].myProperty}}</YOUR_TAG>

1 Comment

Whilst this code snippet is welcome, and may provide some help, it would be greatly improved if it included an explanation of how it addresses the question. Without that, your answer has much less educational value - remember that you are answering the question for readers in the future, not just the person asking now! Please edit your answer to add explanation, and give an indication of what limitations and assumptions apply.

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.