首页 > 其他 > 详细

[Angular 2] 3. RC7: *ngFor

时间:2016-09-14 16:49:44      阅读:481      评论:0      收藏:0      [点我收藏+]

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;
}

技术分享

[Angular 2] 3. RC7: *ngFor

原文:http://www.cnblogs.com/Answer1215/p/5872646.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!