Angular 2 ngfor first, last, index loop

后端 未结 3 2013
耶瑟儿~
耶瑟儿~ 2021-01-30 05:00

I\'m trying to set as default the first occurrence in this example: plunkr

getting the following error:

    Unhandle         


        
相关标签:
3条回答
  • 2021-01-30 05:25

    By this you can get any index in *ngFor loop in ANGULAR ...

    <ul>
      <li *ngFor="let object of myArray; let i = index; let first = first ;let last = last;">
        <div *ngIf="first">
           // write your code...
        </div>
    
        <div *ngIf="last">
           // write your code...
        </div>
      </li>
    </ul>
    

    We can use these alias in *ngFor

    • index : number : let i = index to get all index of object.
    • first : boolean : let first = first to get first index of object.
    • last : boolean : let last = last to get last index of object.
    • odd : boolean : let odd = odd to get odd index of object.
    • even : boolean : let even = even to get even index of object.
    0 讨论(0)
  • 2021-01-30 05:26

    Check out this plunkr.

    When you're binding to variables, you need to use the brackets. Also, you use the hashtag when you want to get references to elements in your html, not for declaring variables inside of templates like that.

    <md-button-toggle *ngFor="let indicador of indicadores; let first = first;" [value]="indicador.id" [checked]="first"> 
    ...
    

    Edit: Thanks to Christopher Moore: Angular exposes the following local variables:

    • index
    • first
    • last
    • even
    • odd
    0 讨论(0)
  • 2021-01-30 05:42

    Here is how its done in Angular 6

    <li *ngFor="let user of userObservable ; first as isFirst">
       <span *ngIf="isFirst">default</span>
    </li>
    

    Note the change from let first = first to first as isFirst

    0 讨论(0)
提交回复
热议问题