码迷,mamicode.com
首页 > 其他好文 > 详细

[Angular 2] 3. RC7: *ngFor

时间:2016-09-14 16:50:03      阅读:107      评论: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
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!