3
 <template ngFor let-item [ngForOf]="faculytLectureMaster"  >
        <tr *ngIf="item.subjectname != 'Break' && item.facultyname != 'NA'" (click)="OnLectureClick(item,0)" 
             [class.selected]="item === _selectedHero" >
            <td> **set Index number here** </td>
            <td>{{item.lectstart}}-{{item.lectend}}</td>                
            <td>{{item.facultyname}}</td>
            <td>{{item.subjectname}}({{item.subjectcode}})</td>             
            <td>{{item.attendtotal}}</td>
            <td>{{item.present}}</td>
            <td>{{item.absent}}</td>
            <td>{{item.section}}</td>
        </tr>
    </template>

I want to set Index for this template in angular2 like $Index in angular1

I searched I got solutions like *ngFor="let item of _studentList let i=index" which work for normal but in template how to use?

how to do it?

User
  • 1,334
  • 5
  • 29
  • 61
  • 1
    Possible duplicate of [ngFor with index as value in attribute](http://stackoverflow.com/questions/35405618/ngfor-with-index-as-value-in-attribute) – sandrooco Jan 16 '17 at 11:06
  • I used but in my case I use template its not working I tried like **[ngForOf]="faculytLectureMaster let i = index" ** but not showing output – User Jan 16 '17 at 11:09

1 Answers1

2

If you define index like let-i=index, in this case in your ngFor, you could print the index with: {{i}}

Try this one, should work:

 <template ngFor let-item [ngForOf]="faculytLectureMaster" let-i="index" >
        <tr *ngIf="item.subjectname != 'Break' && item.facultyname != 'NA'" (click)="OnLectureClick(item,0)" 
             [class.selected]="item === _selectedHero" >
            <td>{{i}}</td>
            <td>{{item.lectstart}}-{{item.lectend}}</td>                
            <td>{{item.facultyname}}</td>
            <td>{{item.subjectname}}({{item.subjectcode}})</td>             
            <td>{{item.attendtotal}}</td>
            <td>{{item.present}}</td>
            <td>{{item.absent}}</td>
            <td>{{item.section}}</td>
        </tr>
    </template>

More info here

AT82
  • 71,416
  • 24
  • 140
  • 167
  • how to use **[ngForOf]="faculytLectureMaster"** here – User Jan 16 '17 at 11:07
  • Updated answer :) – AT82 Jan 16 '17 at 11:11
  • showing error like **Unhandled Promise rejection: Template parse errors: Parser Error: Unexpected token = at column 37 in [let item of facultyLectureMaster; i = index]** – User Jan 16 '17 at 11:11
  • Yeah, sorry, my bad... it should be `let` in front of `i` as well: ` – AT82 Jan 16 '17 at 11:12
  • @Pravin I updated my answer, just now noticed you wanted ngForOf specifically! :) – AT82 Jan 16 '17 at 11:28
  • it work fine but problem it calculate hidden row because this **ngIf="item.subjectname != 'Break' && item.facultyname != 'NA'"** condition – User Jan 16 '17 at 11:58