ChangeDection

检测程序内部状态,然后反映到UI上。

引起状态变化:Events,XHR,Timers

ApplicationRef监听NgZone的onTurnDone,然后执行检测。

OnPush状态完全由外部决定,内部不会去改变状态。

例子:

聪明组件project-list变成OnPush检查策略,

在需要检测时候使用cd.markForCheck).

  1. @Component({
  2. selector: "app-project-list",
  3. templateUrl: "./project-list.component.html",
  4. styleUrls: ["./project-list.component.scss"],
  5. animations:[
  6. slideToRight,listAnimation
  7. ],
  8. changeDetection: ChangeDetectionStrategy.OnPush
  9. })

手动告诉Angualr你来检查我

在事件发生的时候主动告诉Angular来检查这条路线。

  1. import { Component, OnInit , HostBinding, ChangeDetectionStrategy, ChangeDetectorRef } from "@angular/core";
  2. import { MatDialog } from "@angular/material";
  3. import { NewProjectComponent } from "../new-project/new-project.component";
  4. import { InviteComponent } from '../invite/invite.component';
  5. import { ConfirmDialogComponent } from '../../shared/confirm-dialog/confirm-dialog.component';
  6. import {slideToRight} from '../../animate/router.animate'
  7. import { listAnimation } from '../../animate/list.animate';
  8. import { projection } from '@angular/core/src/render3';
  9.  
  10. @Component({
  11. selector: "app-project-list",
  12. templateUrl: "./project-list.component.html",
  13. styleUrls: ["./project-list.component.scss"],
  14. animations:[
  15. slideToRight,listAnimation
  16. ],
  17. changeDetection: ChangeDetectionStrategy.OnPush
  18. })
  19. export class ProjectListComponent implements OnInit {
  20. @HostBinding('@routeAnim') state;
  21.  
  22. projects = [
  23. {
  24. id:1,
  25. name: "企业协作平台",
  26. desc: "这是一个企业内部项目",
  27. coverImg: "assets/images/covers/0.jpg"
  28. },
  29. {
  30. id:2,
  31. name: "自动化测试项目",
  32. desc: "这是一个企业内部项目",
  33. coverImg: "assets/images/covers/2.jpg"
  34. }
  35. ];
  36. constructor(private dialog: MatDialog, private cd:ChangeDetectorRef) { }
  37.  
  38. ngOnInit() { }
  39.  
  40. openNewProjectDialog() {
  41. // this.dialog.open(NewProjectComponent,{data:'this is a dialog'});
  42. const dialogRef = this.dialog.open(NewProjectComponent, {
  43. data: { title: '新建项目' }
  44. });
  45. dialogRef.afterClosed().subscribe((result) => {
  46. console.log(result);
  47. this.projects = [...this.projects,
  48. {id:3,name:'一个新项目',desc:'这是一个新项目',coverImg:"assets/images/covers/3.jpg"},
  49. {id:4,name:'又一个新项目',desc:'这是又一个新项目',coverImg:"assets/images/covers/4.jpg"}]
  50. });
  51. this.cd.markForCheck();
  52. }
  53.  
  54. lauchInviteDialog() {
  55. const dialogRef = this.dialog.open(InviteComponent);
  56. }
  57.  
  58. lauchUpdateDialog() {
  59. const dialogRef = this.dialog.open(NewProjectComponent, {
  60. data: { title: '编辑项目' }
  61. });
  62. }
  63.  
  64. lauchConfimDialog(project) {
  65. const dialogRef = this.dialog.open(ConfirmDialogComponent, {
  66. data: { title: '删除项目', content: '您确认删除该项目吗?' }
  67. });
  68. dialogRef.afterClosed().subscribe(result=>{
  69. console.log(result);
  70. this.projects=this.projects.filter(p=>p.id!=project.id);
  71. this.cd.markForCheck();
  72. });
  73. }
  74. }

把笨组件标识为OnPush

直接加changeDetection:ChangeDetectionStrategy.OnPush

  1. @Component({
  2. selector: 'app-new-project',
  3. templateUrl: './new-project.component.html',
  4. styleUrls: ['./new-project.component.scss'],
  5. changeDetection:ChangeDetectionStrategy.OnPush
  6. })

Angular changeDetction的更多相关文章

  1. Angular杂谈系列1-如何在Angular2中使用jQuery及其插件

    jQuery,让我们对dom的操作更加便捷.由于其易用性和可扩展性,jQuer也迅速风靡全球,各种插件也是目不暇接. 我相信很多人并不能直接远离jQuery去做前端,因为它太好用了,我们以前做的东西大 ...

  2. Angular企业级开发(5)-项目框架搭建

    1.AngularJS Seed项目目录结构 AngularJS官方网站提供了一个angular-phonecat项目,另外一个就是Angular-Seed项目.所以大多数团队会基于Angular-S ...

  3. TypeScript: Angular 2 的秘密武器(译)

    本文整理自Dan Wahlin在ng-conf上的talk.原视频地址: https://www.youtube.com/watch?v=e3djIqAGqZo 开场白 开场白主要分为三部分: 感谢了 ...

  4. angular实现统一的消息服务

    后台API返回的消息怎么显示更优雅,怎么处理才更简洁?看看这个效果怎么样? 自定义指令和服务实现 自定义指令和服务实现消息自动显示在页面的顶部,3秒之后消失 1. 显示消息 这种显示消息的方式是不是有 ...

  5. div实现自适应高度的textarea,实现angular双向绑定

    相信不少同学模拟过腾讯的QQ做一个聊天应用,至少我是其中一个. 过程中我遇到的一个问题就是QQ输入框,自适应高度,最高高度为3row. 如果你也像我一样打算使用textarea,那么很抱歉,你一开始就 ...

  6. Angular企业级开发-AngularJS1.x学习路径

    博客目录 有链接的表明已经完成了,其他的正在建设中. 1.AngularJS简介 2.搭建Angular开发环境 3.Angular MVC实现 4.[Angular项目目录结构] 5.[SPA介绍] ...

  7. Angular企业级开发(4)-ngResource和REST介绍

    一.RESTful介绍 RESTful维基百科 REST(表征性状态传输,Representational State Transfer)是Roy Fielding博士在2000年他的博士论文中提出来 ...

  8. Angular企业级开发(3)-Angular MVC实现

    1.MVC介绍 Model-View-Controller 在20世纪80年代为程序语言Smalltalk发明的一种软件架构.MVC模式的目的是实现一种动态的程序设计,使后续对程序的修改和扩展简化,并 ...

  9. Angular企业级开发(2)-搭建Angular开发环境

    1.集成开发环境 个人或团队开发AngularJS项目时,有很多JavaScript编辑器可以选择.使用优秀的集成开发环境(Integrated Development Environment)能节省 ...

随机推荐

  1. Ubuntu terminal colors

    Today I run ubuntu docker image on powershell and find the directory color is blue, so I want to cha ...

  2. 20165223 《信息安全系统设计基础》 改进ls的实现

    课下作业:改进ls的实现 一.作业要求 参见附图,改进你的ls的实现.提交代码运行截图和码云链接.

  3. Python学习day17 迭代器&生成器

    迭代器&生成器 1. 迭代器 1.1 迭代器 迭代:迭代是重复反馈过程的活动,其目的通常是为了逼近所需目标或结果.每一次对过程的重复称为一次"迭代" 迭代器:帮助对某种对象 ...

  4. CF 1145 (愚人节比赛)

    D题 题目就是让你找出题面中拼错的单词,拼错的字母组合成一句话就是正确的题面. two plus xor of third and min elements #include<bits/stdc ...

  5. NOI2009 管道取珠 神仙DP

    原题链接 原题让求的是\(\sum\limits a_i^2\),这个东西直接求非常难求.我们考虑转化一下问题. 首先把\(a_i^2\)拆成\((1+1+...+1)(1+1+...+1)\),两个 ...

  6. 转载:curl 模拟请求

    一般情况下我们会在网页上请求后台接口,但是对于需要进行多次测试的人来说,每一次都要在网页上模拟请求,是存在很大局限性的.因此,我们需要学会模拟请求,以达到跟实际请求一样的效果. 1. curl的用法 ...

  7. Windows系统CMD下常用命令

    命令    功能ASSOC    显示或修改文件扩展名关联.ATTRIB    显示或更改文件属性.BREAK    设置或清除扩展式 CTRL+C 检查.BCDEDIT    设置启动数据库中的属性 ...

  8. 将图片转为base64

    DEMO: <input type="file" id="file" multiple="multiple"> <div ...

  9. JSON循环遍历解析

    使用递归方式遍历JSON,解析JSON用的是:net.sf.json, alibaba.fastjson测试可用 @Test public void test() { String json = &q ...

  10. OCC上下文设置显示模式

    #include <AIS_InteractiveContext.hxx> 通过AIS_InteractiveContext::SetDisplayMode()函数来设置 void  Se ...