For example you make a function to get rating;

getRating(score: number): string {
let rating: string;
console.count('RatingPipe');
if(score > 249000){
rating = "Daniel Boone";
}
else if(score > 200000){
rating = "Trail Guide";
}
else if(score > 150000){
rating = "Adventurer";
}
else if(score > 100000){
rating = "Pioneer";
}
else if(score > 50000){
rating = "Greenhorn";
}
else{
rating = "Buzzard food";
}
return rating;
}

Then using it in html:

{{getRating(entry.points)}}

These code actually casues the preformance issues, because everything Angualr's change detection run, it saw function call inside {{}}, it have to run it everything when anything changes, there is no way to figure out whether the function output changes or not without running it.

The way to fix it is using Pipe. Angular will remember the input value and cache the output. Therefore by using pipe we can reduce the number of function call way better.

import { Pipe, PipeTransform } from '@angular/core';

@Pipe({
name: 'Rating'
})
export class ScoreRatingPipe implements PipeTransform { transform(score: number): string {
let rating: string;
console.count('RatingPipe');
if(score > 249000){
rating = "Daniel Boone";
}
else if(score > 200000){
rating = "Trail Guide";
}
else if(score > 150000){
rating = "Adventurer";
}
else if(score > 100000){
rating = "Pioneer";
}
else if(score > 50000){
rating = "Greenhorn";
}
else{
rating = "Buzzard food";
}
return rating;
} }
{{entry.points | Rating }}

最新文章

  1. cglib动态代理
  2. ElasticSearch大数据分布式弹性搜索引擎使用
  3. 史上最强大的js图表库——ECharts带你入门(转)
  4. hibernate的多对多例子讲解(加图片)
  5. Jenkins运行完Test后,把ngreport生成的测试报告 拷贝到相应的文件夹
  6. SQLServer排序时与读取的记录会影响到结果?
  7. 【转】python中常用第三方包os sys
  8. Maven之多模块打包成一个jar包及assembly
  9. PHP DES加密
  10. 【转】nginx配置:location配置方法及实例详解
  11. 添加OpenStack Mitaka源
  12. 毕业论文内容框架指导-适用于MIS系统
  13. iOS----------取数据的两种取法
  14. 数据库的Connection、Cursor两大对象
  15. 利用curl 实现URL监控
  16. type=number 的maxlength和可以输入E的问题
  17. SharePoint Online 怎样让用户只能拥有sub site 的权限
  18. TZOJ 4085 Drainage Ditches(最大流)
  19. python接口自动化2-发送post请求详解(二)
  20. 语法规范:BNF与ABNF 巴斯克范式

热门文章

  1. c++ valarray 实现矩阵与向量相乘
  2. openstack 性能优化极致
  3. 用Thymeleaf在前台下拉列表取值
  4. 关于MFC控件删除出现“具有该ID的控件已存在”这样的情况的解决方案,详细,网上都没有这么详细的,我是“深受其害”,所以想将详细的方法分享出去。
  5. qW3xT.2挖矿病毒 解决过程及坑
  6. sklearn中的数据预处理和特征工程
  7. Android插件化原理解析——Hook机制之动态代理
  8. Java中数组要点总结
  9. 金立 M6 (GN8003) 解锁 BootLoader 进入第三方 recovery 刷机 ROOT
  10. VTK嵌入MFC同步显示