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的更多相关文章

  1. Angular 4+ 修仙之路

    Angular 4.x 快速入门 Angular 4 快速入门 涉及 Angular 简介.环境搭建.插件表达式.自定义组件.表单模块.Http 模块等 Angular 4 基础教程 涉及 Angul ...

  2. Angular学习资料大全和常用语法汇总(让后端程序员轻松上手)

    前言: 首先为什么要写这样的一篇文章呢?主要是因为前段时间写过一些关于Angualr的相关实战文章,有些爱学习的小伙伴对这方面比较感兴趣,但是又不知道该怎么入手(因为认识我的大多数小伙伴都是后端的同学 ...

  3. [Angular 2] More on *ngFor, @ContentChildren & QueryList<>

    In previous artical, we introduce the how to use *ngFor. The limitation for previous solution to dis ...

  4. [Angular 2] *ngFor

    heros.ts: import {Component} from "@angular/core"; const HEROES = [ {id: 1, name:'Superman ...

  5. 在Angular中利用trackBy来提升性能

    在Angular的模板中遍历一个集合(collection)的时候你会这样写: <ul> <li *ngFor="let item of collection"& ...

  6. [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 ...

  7. [Angular] Create a simple *ngFor

    In this post, we are going to create our own structure directive *ngFor. What it should looks like i ...

  8. angular 中数据循环 *ngFor

    <!--The content below is only a placeholder and can be replaced.--> <div style="text-a ...

  9. [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 ...

随机推荐

  1. hiho #1066 : 无间道之并查集

    #1066 : 无间道之并查集 时间限制:20000ms 单点时限:1000ms 内存限制:256MB 描述 这天天气晴朗.阳光明媚.鸟语花香,空气中弥漫着春天的气息……额,说远了,总之,小Hi和小H ...

  2. 【Python数据分析】用户通话行为分析

    主要工作: 1.对从网上营业厅拿到的用户数据.xls文件,通过Python的xlrd进行解析,计算用户的主叫被叫次数,通话时间,通话时段. 2.使用matplotlib画图包,将分析的结果直观的绘制出 ...

  3. vue中使用echarts(vue+vue-cli+axios+jsonp+echarts)

    一.安装echarts: cnpm i echarts -D 二.在vue-cli的main.js文件中引用echarts: import charts from 'echarts' Vue.prot ...

  4. echarts 添加自定义label标签

    1.echarts 自定义标签 注:当设置visualMap的后,给覆盖regions单独定义的值(如果data 中没有regions的地区 则无妨,我这个是从data中删除'青岛',但是lable ...

  5. 学习springboot(三)——springboot+mybatis出现org.apache.ibatis.binding.BindingException: Invalid bound state

    有段时间没搭建过了生疏了,记录下出现此情况且你能通过注解的方式正常进行数据库操作,只是通过mapper.xml不行就可以看看这个了.主要问题应该是配置上,不要太自信自己,再仔细找找.1.查看xml是否 ...

  6. hdu 1051 wooden sticks (贪心+巧妙转化)

    #include <iostream>#include<stdio.h>#include<cmath>#include<algorithm>using ...

  7. sh_10_字典基本使用

    sh_10_字典基本使用 xiaoming_dict = {"name": "小明"} # 1. 取值 print(xiaoming_dict["na ...

  8. 分享几个免费IP地址查询接口(API)

    淘宝IP地址库 提供的服务包括:1. 根据用户提供的IP地址,快速查询出该IP地址所在的地理信息和地理相关的信息,包括国家.省.市和运营商.2. 用户可以根据自己所在的位置和使用的IP地址更新我们的服 ...

  9. JavaWeb_使用dom4j解析、生成XML文件

    dom4j 官网 xml解析DOM文档对象模型(树形结构) DOM方式解析:把xml文档加载到内存形成树形结构,可以进行增删改的操作 Learn   使用dom4j解析文件"NewFile. ...

  10. MySQL 建表时 date 类型的默认值设置

    在执行下面 SQL 语句时发现报错 CREATE TABLE `jc_site_access_pages` ( `access_date` date NOT NULL DEFAULT '0000-00 ...