Angular 2 ngFor vs Angular 1 ng-repeat
一直想写关于 Angular 1.x 与 Angular 2.x (Angular 4.x 已发布) 区别的文章,方便 Angular 1.x 的用户快速的过渡到 Angular 2.x。在浏览文章的时候,发现 Todd Motto 大神,已经写了相关的系列文章。英文好的同学,建议直接阅读 From ng-repeat in Angular 1.x to ngFor in Angular 2 原文哈,因为我并不打算完整地翻译。废话不多说,接下来我们开始进入正题。 目录
Angular 1.xUsing ng-repeat在使用 const app = { controller() { this.groceries = [{ id: 0,label: 'Butter' },{ id: 1,label: 'Apples' },{ id: 2,label: 'Paprika' },{ id: 3,label: 'Potatoes' },{ id: 4,label: 'Oatmeal' },{ id: 5,label: 'Spaghetti' },{ id: 6,label: 'Pears' },{ id: 7,label: 'Bacon' }]; } }; angular .module('app') .component('app',app); 接下来我们在组件模板中,使用 const app = { template: ` <div> Grocery selected: {{ $ctrl.selectedGrocery.label }} <ul> <li ng-repeat="grocery in $ctrl.groceries"> <a href="" ng-click="$ctrl.selectGrocery(grocery);"> {{ grocery.label }} </a> </li> </ul> </div> `,... }; Using $index and track by$index 表示数组中每一项的索引值,除了 $index 之外,ng-repeat 还导出 const app = { template: ` ... <li ng-repeat="grocery in $ctrl.groceries"> <a href="" ng-click="$ctrl.selectGrocery(grocery);"> {{ grocery.label }} {{ $index }} </a> </li> ... `,... }; 在设置 const app = { template: ` ... <li ng-repeat="grocery in $ctrl.groceries track by grocery.id"> <a href="" ng-click="$ctrl.selectGrocery(grocery);"> {{ grocery.label }} {{ $index }} </a> </li> ... `,... }; 此外 track by 也支持函数表达式: const app = { template: ` ... <li ng-repeat="grocery in $ctrl.groceries track by trackByGrocery(grocery)"> <a href="" ng-click="$ctrl.selectGrocery(grocery);"> {{ grocery.label }} {{ $index }} </a> </li> ... `,... }; Angular 2.xAngular 2.x 中不存在 Using ngForinterface Grocery { id: number; label: string; } export default class AppComponent { public groceries: Grocery[]; public selectedGrocery: Grocery; constructor() { this.groceries = [{ id: 0,label: 'Bacon' }]; this.selectGrocery(this.groceries[0]); } selectGrocery(grocery: Grocery) { this.selectedGrocery = grocery; } } 设置好初始化数据,接下来我们来使用 import { Component } from '@angular/core'; interface Grocery { id: number; label: string; } @Component({ selector: 'exe-app',template: ` <div> Grocery selected: {{ selectedGrocery.label }} <ul> <li *ngFor="let grocery of groceries;"> <a href="#" (click)="selectGrocery(grocery);"> {{ grocery.label }} </a> </li> </ul> </div> ` }) export class AppComponent { public groceries: Grocery[]; public selectedGrocery: Grocery; constructor() { this.groceries = [{ id: 0,label: 'Bacon' }]; this.selectGrocery(this.groceries[0]); } selectGrocery(grocery: Grocery) { this.selectedGrocery = grocery; } } 细心的读者,可能会注意到模板中 Using index and trackBy在 Angular 1.x 中我们可以直接 $index 访问到列表项的索引值,但在 Angular 2 中,我们使用 index 之前,我们必须先把它赋值给其它变量,具体示例如下: @Component({ selector: 'exe-app',template: ` <div> Grocery selected: {{ selectedGrocery.label }} <ul> <li *ngFor="let grocery of groceries; let i = index;"> <a href="#" (click)="selectGrocery(grocery);"> {{ grocery.label }} {{ i }} </a> </li> </ul> </div> ` }) export default class AppComponent {...} 介绍完 ngFor 中的 @Component({ selector: 'exe-app',template: ` <div> Grocery selected: {{ selectedGrocery.label }} <ul> <li *ngFor="let grocery of groceries; let i = index; trackBy: trackByGrocery;"> <a href="#" (click)="selectGrocery(grocery);"> {{ grocery.label }} {{ i }} </a> </li> </ul> </div> ` }) export default class AppComponent { ... trackByGrocery: (index: number,grocery: Grocery): number => grocery.id; ... } 完整示例如下: import { Component } from '@angular/core'; interface Grocery { id: number; label: string; } @Component({ selector: 'exe-app',template: ` <div> Grocery selected: {{ selectedGrocery.label }} <ul> <li *ngFor="let grocery of groceries; let i = index; trackBy: trackByGrocery;"> <a href="#" (click)="selectGrocery(grocery);"> {{ grocery.label }} {{ i }} </a> </li> </ul> </div> ` }) export class AppComponent { public groceries: Grocery[]; public selectedGrocery: Grocery; constructor() { this.groceries = [{ id: 0,label: 'Bacon' }]; this.selectGrocery(this.groceries[0]); } selectGrocery(grocery: Grocery) { this.selectedGrocery = grocery; } trackByGrocery(index: number,grocery: Grocery): number { return grocery.id } } 我有话说1.Angular 2 指令有几种分类? 在 Angular 2 指令分为三类:
详细内容请参考 - Angular 2 Directive 2.Angular 2 中的模板语法有哪一些? Angular 2 中没有 Angular 1.x 2.1 属性绑定: <show-title [title]="title"></show-title> 2.2 事件绑定: <a href="#" (click)="selectGrocery(grocery);"> {{ grocery.label }} {{ i }} </a> 详细内容请参考 - Angular 2 template syntax & common directives (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |