0

How to display data in HTML from a list without using *ngFor for example like This list[5].name

I tried this :

PostList[5].{{def}}

{{PostList[5].def}}
3
  • do you mean *ngFor? Commented Dec 14, 2017 at 13:33
  • Why wouldn't you want to use ngFor or ngRepeat? Commented Dec 14, 2017 at 13:35
  • They were basically asking, how can you retrieve an object in an array given a specific index in html. They are avoiding having to iterate through the array. Commented Dec 14, 2017 at 16:26

2 Answers 2

2

In your case {{PostList[5].def}} works.

Assuming you have your component initializing a simple list with an Object

export class AppComponent  {
  list = new Array<{name: string}>();

  constructor() {
    this.list[0] = {"name": "Martin"}
    this.list[1] = {"name": "Emma"}
    this.list[2] = {"name": "Daisy"}
  }
}

and if you want to get Emma, your view will therefore be.

<p>
  {{list[1].name}}
</p>

//Output Emma

Update I would like to add that, if for instance the list doesnt have a certain index e.g index greater than 2, or the value is null. You can use the ? operator in html to be safe.

Therefore {{list[3]?.name}} wont crash your app, even though the output is null.

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

Comments

0

{{PostList[5].def}} should work if you have this.PostList variable in your component (and it is an array which 6th element is an object with def)

Comments

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.