5

I have an array that i want to display from it the first item in a span .

At the moment i'm getting all the value instead of only the first one.

<div class="card">  
  <div *ngIf="selectedUser._id">
    <div class="user" *ngFor="let user of users">
       <span> {{ user.event }} </span>
    </div>
  </div>
</div>

It's returning me the list of all users date event, i only want the first one

I also tried to add user.event[0] not working it's displaying me the first char of the date

my array of object

[
   {
      id:XYZ, 
      event: Fri Jul 20 2018 15:00:04 GMT+0200 (CEST)
   }, {
      id:XYZ1, 
      name: Fri Jul 10 2018 15:00:04 GMT+0200 (CEST)
   }, {
      id:XYZ2, 
      name: Fri Aug 20 2018 15:00:04 GMT+0200 (CEST)
   }
]

I tried this solution that is working and achieving what i want

<div class="message" *ngFor="let user of users | slice:0:1;"> 

But I know this is looping through the array, I need to get the first element without doing it.

I already tried this :

<span> {{ users[0].event }} </span>

it's throwing me an error cause sometimes the event is undefined

2
  • 3
    users[0]?.event Commented Aug 9, 2018 at 8:51
  • chances are big that you want to make the entire div or span conditional to the existence of users[0], which leads me to the conclusion that you want to centralize it in a method firstUser, see below. Commented Aug 9, 2018 at 9:31

4 Answers 4

3

You should check if array item is empty or not

<span> {{ users && users.length ? users[0].event : '' }} </span>
Sign up to request clarification or add additional context in comments.

Comments

2

Use the safe navigation operator i.e. ? to make sure the users array is checked for undefined.

The Angular safe navigation operator (?.) is a more fluent and convenient way to guard against nulls in property paths. The expression bails out when it hits the first null value. The display is blank, but the app keeps rolling without errors.

<span *ngIf="users[0]">
    {{ users[0]?.event }}
</span>

Link to StackBlitz Demo.

3 Comments

the question remains, do you even want an empty span if there is no user. Perhaps you don't want to print the span all together in that case. Or perhaps you want to print "no user found"
@bvdb add an *ngIf then, see the updated answer and demo.
indeed, but then you don't need the ? operator, right ?
2

Just try this:

<span> {{ users[0]?.event }} </span> 

Comments

0

I think the simplest solution will be to write a method firstUser() in your Component class.

Your typescript file should contain

firstUser () {
  return this.users[0];
}

This just keeps your html section as clean as possible. Chances are big that you will need to use it twice anyway:

    <div class="user" *ngIf="firstUser" >
         <span>  {{ firstUser.event }} </span>
    </div>

You may even want to print a message like "no user found" instead.

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.