angular - ngFor, trackby
ngFor
ngForOf指令通常使用缩写形式*ngFor为集合中的每个项呈现模板的结构指令。该指令放置在元素上,该元素将成为克隆模板的父级。
<li *ngFor="let item of items; index as i; trackBy: trackByFn">...</li>
一般使用是:
const list = [{age: '16'}, {age: '18'}, {age: '15'}] <li *ngFor="let item of list; index as i>{{item.age}}</li>
// 或者
<li *ngFor="let item of list; let i = index">{{item.age}}</li>
使用trackBy提高性能
trackBy: trackByFn 定义如何跟踪iterable中项的更改的函数。
在iterable中添加、移动或移除项时,指令必须重新呈现适当的DOM节点。为了最小化DOM中的搅动,只重新呈现已更改的节点。
默认情况下,更改检测器假定对象实例标识iterable中的节点。提供此函数时,指令使用调用此函数的结果来标识项节点,而不是对象本身的标识。
函数接收两个输入,迭代索引和节点对象ID。
要想自定义默认的跟踪算法,NgForOf
支持 trackBy
选项。 trackBy
接受一个带两个参数(index
和 item
)的函数。
如果给出了 trackBy
,Angular 就会使用该函数的返回值来跟踪变化。
@Component({
selector: 'my-app',
template: `
<ul>
<li *ngFor="let item of list;">{{item.age}}</li>
<li *ngFor="let item of list;trackBy: trackByFn">{{item.age}}</li>
</ul>
<button (click)="getItems()">Refresh items</button>
`,
})
export class App {
list = [{age: '16'}, {age: '18'}, {age: '15'}]; constructor() {
this.list= [{age: '16'}, {age: '18'}, {age: '15'}]
} getItems() {
this.list= [{age: '16'}, {age: '18'}, {age: '15'}, {age: '80'}]
} trackByFn(index, item) {
return item.age; // or index
}
}
列表发生变化是,如果没有添加 trackBy , 那么与数据关联度的所有DOM元素会重新渲染;
如果使用trackBy :更改列表时,Angular可以根据唯一标识符跟踪已添加或删除的项目,并仅创建或销毁已更改的项目。
局部变量
const list = [{age: '16'}, {age: '18'}, {age: '15'}];
<li *ngFor="let item of list; first as isFirst">{{item.age}} <span *ngIf="isFirst">岁</span> </li>
/*
16岁
18
15
*/
NgForOf
导出了一系列值,可以指定别名后作为局部变量使用:
$implicit: T
:迭代目标(绑定到ngForOf
)中每个条目的值。ngForOf: NgIterable<T>
:迭代表达式的值。当表达式不局限于访问某个属性时,这会非常有用,比如在使用async
管道时(userStreams | async
)。index: number
:可迭代对象中当前条目的索引。first: boolean
:如果当前条目是可迭代对象中的第一个条目则为true
。last: boolean
:如果当前条目是可迭代对象中的最后一个条目则为true
。even: boolean
:如果当前条目在可迭代对象中的索引号为偶数则为true
。odd: boolean
:如果当前条目在可迭代对象中的索引号为奇数则为true
。
angular - ngFor, trackby的更多相关文章
- Angular 4+ 修仙之路
Angular 4.x 快速入门 Angular 4 快速入门 涉及 Angular 简介.环境搭建.插件表达式.自定义组件.表单模块.Http 模块等 Angular 4 基础教程 涉及 Angul ...
- Angular学习资料大全和常用语法汇总(让后端程序员轻松上手)
前言: 首先为什么要写这样的一篇文章呢?主要是因为前段时间写过一些关于Angualr的相关实战文章,有些爱学习的小伙伴对这方面比较感兴趣,但是又不知道该怎么入手(因为认识我的大多数小伙伴都是后端的同学 ...
- [Angular 2] More on *ngFor, @ContentChildren & QueryList<>
In previous artical, we introduce the how to use *ngFor. The limitation for previous solution to dis ...
- [Angular 2] *ngFor
heros.ts: import {Component} from "@angular/core"; const HEROES = [ {id: 1, name:'Superman ...
- 在Angular中利用trackBy来提升性能
在Angular的模板中遍历一个集合(collection)的时候你会这样写: <ul> <li *ngFor="let item of collection"& ...
- [Angular 2] Using ng-for to repeat template elements
This lesson covers Angular 2’s version of looping through data in your templates: ng-for. It’s conce ...
- [Angular] Create a simple *ngFor
In this post, we are going to create our own structure directive *ngFor. What it should looks like i ...
- angular 中数据循环 *ngFor
<!--The content below is only a placeholder and can be replaced.--> <div style="text-a ...
- [Angular 2] ng-model and ng-for with Select and Option elements
You can use Select and Option elements in combination with ng-for and ng-model to create mini-forms ...
随机推荐
- JQuery的deferred.promise()
jQuery提供的deferred.promise()方法的作用是,在原来的Deferred 对象上返回另一个 Deferred 对象,即受限制的 Promise 对象,受限制的 Promise 对象 ...
- wget下载与tar压缩/解压
目录 wget命令 下载整个网站 压缩与解压 小节 wget命令 Usage: wget [OPTION]... [URL]... # 后台运行 -b, --background go to back ...
- 基于官方postgres docker镜像制作自己的镜像
1.Dockerfile FROM library/postgres MAINTAINER wenbin.ouyang #初始化PostgreSQL ENV POSTGRES_USER root EN ...
- Nowcoder 北师校赛 B 外挂使用拒绝 ( k次前缀和、矩阵快速幂打表找规律、组合数 )
题目链接 题意 : 中文题.点链接 分析 : 有道题是问你不断求前缀和后的结果 Click here 这道题问的是逆过程 分析方法雷同.可参考 Click here ----------------- ...
- [AGC034D]Manhattan Max Matching:费用流
前置姿势 \(k\)维空间内两点曼哈顿距离中绝对值的处理 戳这里:[CF1093G]Multidimensional Queries 多路增广的费用流 据说这个东西叫做ZKW费用流? 流程其实很简单, ...
- 9.Python关键字(保留字)一览表
保留字是 Python 语言中一些已经被赋予特定意义的单词,这就要求开发者在开发程序时,不能用这些保留字作为标识符给变量.函数.类.模板以及其他对象命名. Python 包含的保留字可以执行如下命令进 ...
- 隐藏表单域、URL重写、cookie、session
隐藏表单域: 隐藏域是用来收集或发送信息的不可见元素,对于网页的访问者来说,隐藏域是看不见的.当表单被提交时,隐藏域就会将信息用你设置时定义的名称和值发送到服务器上. 代码格式:<input t ...
- [BZOJ3611][Heoi2014]大工程(虚树上DP)
3611: [Heoi2014]大工程 Time Limit: 60 Sec Memory Limit: 512 MBSubmit: 2464 Solved: 1104[Submit][Statu ...
- @transient 注解 和 transient变量的作用
@transient 和 transient是两码事 1.@transient的作用 @transient是hibernate和Morphia中的注解,hibernate都熟悉,Morphia是通过同 ...
- WebSocket 结合 Nginx 实现域名及 WSS 协议访问-Nginx配置
特别提示:本人博客部分有参考网络其他博客,但均是本人亲手编写过并验证通过.如发现博客有错误,请及时提出以免误导其他人,谢谢!欢迎转载,但记得标明文章出处:http://www.cnblogs.com/ ...