首页 > 代码库 > [Angular 2] 3. RC7: *ngFor
[Angular 2] 3. RC7: *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;}
[Angular 2] 3. RC7: *ngFor
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。