Angel Angel
Angel Angel

Reputation: 21658

reference an object within an array of objects in Angular 2 inside of template

Maybe this is a stupid question. After reading several times this https://angular.io/docs/ts/latest/guide/template-syntax.html I can not find a solution, to reference an object within an array of objects.

I tried it with this

{{ users[0] ? users[0].description }}

I obtain this

Parser Error: Conditional expression {{ users[0] ? users[0].description }} requires all 3 expressions at the end of the expression [ {{ users[0] ? users[0].description }} ] in


I tried it with this

{{ users.0 ? users.0.description }}

I obtain this

Unexpected token '0' at column 7 in [ {{ users.0 ? users.0.description }} ]


any of the above syntax is correct and my mistake is elsewhere, or none of the above is correct. Sorry for my English

somewhere in code

..//
  users: Array<Object>;
  ..//
  this.users = new Array();
  ..//
  this.users.push(users.json())
  ..//

Upvotes: 2

Views: 1619

Answers (2)

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

Reputation: 657148

You were probably looking for

{{ users[0]?.description }}

Upvotes: 2

DonovanM
DonovanM

Reputation: 1198

You have to provide the "else" part of the ternary operator. So just change it to:

{{ users[0] ? users[0].description : '' }}

Upvotes: 3

Related Questions