1

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)}]
1
  • where is the date? can use post the structure of users? Commented Aug 3, 2018 at 10:24

4 Answers 4

1

Use the simple approch below,

<div class="card">  
    <div *ngIf="selectedUser._id">
      <div class="user">
         <span>  {{ users[0].event }} </span>
      </div>
    </div>
</div>
Sign up to request clarification or add additional context in comments.

2 Comments

This is the most simple working solution to this as I think
unfortunately tried this i get TypeError: Cannot read property 'date' of undefined
0

you can use slice pipe.

<div class="user" *ngFor="let user of users | slice:0:1;">
    <span>  {{ user.event }} </span>
</div>

Comments

0
<div class="card">
  <div>
    <div class="user">
      <div>{{users[0]}}</div>
    </div>
  </div>
</div> 

1 Comment

stackoverflow.com/help/how-to-answer. Please explain your answer and how it solves the problem in the question.
0

You dont need

<div class="card">  
    <div>
      <div class="user">
             <span>    {{ users[0].event | date:'medium' }} </span>
     </div>
</div>  

STACKBLITZ DEMO

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.