0

I am trying to get first object from the nested json in angular2. Can any one help me in fetching first json object.
Sample Json:

var Datalist = [{"id":1,"status":"completed"},{"id":2,"status":"Not completed"},{"id":3,"status":"Not completed"}]

My angular code:

<ul class="list-group">
<li *ngFor="let c of Datalist"
    class="list-group-item">
    <label> {{c.status}} </label>
         <span class="pull-right">{{c.id}}</span>
</li>
</ul>

Now i am getting all 3 id's and status's. I just want first object,1,completed

4 Answers 4

1

You dont need to use ngFor if you need only the first item, access the first element using 0th index

<ul class="list-group">
<li  class="list-group-item">
    <label> {{Datalist[0].status}} </label>
    <span class="pull-right">{{Datalist[0].id}}</span>
</li>
</ul>
Sign up to request clarification or add additional context in comments.

Comments

1

Try this

<ul class="list-group">
<li class="list-group-item">
    <label> {{Datalist[0].status}} </label>
         <span class="pull-right">{{Datalist[0].id}}</span>
</li>
</ul>

Comments

0

you need to test if the status is completed or not , using ngIf directive :

<ul class="list-group">
<li *ngFor="let c of Datalist"
    class="list-group-item">
    <label *ngIf="c.status==='completed'"> {{c.status}} </label>
    <span class="pull-right" *ngIf="c.status==='completed'">{{c.id}}</span>
</li>
</ul>

Comments

0

Try like this :

<ul class="list-group">
    <span *ngFor="let c of Datalist; let i = index; ">
        <li class="list-group-item" *ngIf="i == 0">
            <label> {{c.status}} </label>
            <span class="pull-right">{{c.id}}</span>
        </li>
    </span>
</ul>

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.