zoukankan      html  css  js  c++  java
  • [Angular 2] *ngFor

    heros.ts:

    import {Component} from "@angular/core";
    
    const HEROES = [
        {id: 1, name:'Superman'},
        {id: 2, name:'Batman'},
        {id: 5, name:'BatGirl'},
        {id: 3, name:'Robin'},
        {id: 4, name:'Flash'}
    ];
    
    @Component({
        selector:'heroes',
        styleUrls: [
            'heroes.component.css'
        ],
        template: `
        <table>
            <thead>
                <th>Name</th>
                <th>Index</th>
            </thead>
            <tbody>
                <tr *ngFor="let hero of heroes; let i = index; trackBy: trackBy(hero);
                 let isEven=even; let isFirst=first; let isLast=last;"
                 [ngClass]="{'even': isEven, 'first': isFirst, 'last': isLast}">
                    <td>{{hero.name}}</td>
                    <td>{{i}}</td>
                </tr>
            </tbody>
        </table>
    `
    })
    export class Heroes {
        heroes = HEROES;
    
        trackBy(hero){
            return hero ? hero.id: undefined;
        }
    }

    here we can also use:

    trackBy: hero?.id

    heroes.component.css:

    .even{
        background: lightgray;
    }
    
    .first{
        font-weight: bold;
    }
    
    .last{
        color: white;
        background: black;
    }

  • 相关阅读:
    WinCE 与通讯模块
    6174问题
    阶乘因式分解(一)
    三个数从小到大排序
    公约数和公倍数
    水仙花数
    韩信点兵
    5个数求最值
    求转置矩阵问题
    孪生素数问题
  • 原文地址:https://www.cnblogs.com/Answer1215/p/5872646.html
Copyright © 2011-2022 走看看