0

How can I ask about the size of an array in angular 2 when I am using it in a component, for example:

users : User[];

with

export class User {
  id : number;
  firstName : String;
  lastName  : String;
  userName  : String
}

and

  <tbody>
    <tr *ngFor="let user of users">
      <th scope="row">{{ user.id }}</th>
      <td>{{ user.firstName }}</td>
      <td>{{ user.lastName }}</td>
      <td>{{ user.userName }}</td>
    </tr>
  </tbody>

what should be the way to ask for the size on array inside a *ngIf expression using interpolation to detect it the size is different to empty?

1 Answer 1

2
<tbody *ngIf="users.length">
    <tr *ngFor="let user of users">
      <th scope="row">{{ user.id }}</th>
      <td>{{ user.firstName }}</td>
      <td>{{ user.lastName }}</td>
      <td>{{ user.userName }}</td>
    </tr>
</tbody>
Sign up to request clarification or add additional context in comments.

2 Comments

thanks! it worked ok, one more question: how do you do make evaluations like "length equal to 3" o "length is different to 2"?
the *ngIf wait a result of a conditionnal comparaison. So you can reuse the same syntax to evaluate other state like users.length == 3 or users.length != 2 and display/hidden other html block.

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.