只有这三种事件才会导致Angular视图的更新,都是异步事件。

  • Events:如 click, change, input, submit 等用户事件
  • XMLHttpRequests:比如从远端服务获取数据
  • Timers: 比如 JavaScript 的自有 API setTimeout(), setInterval()

https://angular.io/guide/lifecycle-hooks

Respond when Angular sets or resets data-bound input properties. The method receives a SimpleChanges object of current and previous property values.
Note that this happens very frequently, so any operation you perform here impacts performance significantly.

看一个例子:子组件实现了Change detect hook:

子组件的bankName, 绑定到了父组件的bankName属性上:

完整源代码:

export class BankAccount implements OnChanges{ngOnChanges(changes: SimpleChanges): void {debugger;}// This property is bound using its original name.@Input() bankName: string;// this property value is bound to a different property name// when this component is instantiated in a template.@Input('account-id') id: string;// this property is not bound, and is not automatically updated by AngularnormalizedBankName: string;
}@Component({selector: 'app',template: `<bank-account [bankName]="bankName" account-id="4747"></bank-account>`
})
export class App implements OnInit, AfterViewInit{_bankName = 'Jerry';ngAfterViewInit(): void {this._bankName = 'Jerry2';}get bankName(){debugger;return this._bankName;}ngOnInit(): void {debugger;}}

父组件的bankName,在OnInit和OnAfterViewInit时都会变化,这也会触发子组件的ngOnChange接口:

第一次触发:

然后父组件的模板第二次被渲染,bankName变成Jerry2:

此时再次refreshView之后,最新的Jerry2和之前的Jerry都记录在SimpleChange数据结构里了:

更多Jerry的原创文章,尽在:“汪子熙”:

Angular ngOnChanges hook学习笔记相关推荐

  1. Angular官网学习笔记

    Angular官网学习笔记 一.Angular概述 **什么是Angular:**一个基于TypeScript构建的开发平台包括: 一个基于组件的框架,用于构建可伸缩的Web应用 一组完美集成的库,涵 ...

  2. PC微信hook学习笔记(一)—— 获取个人信息

    PC微信hook学习笔记(一)-- 获取微信个人信息 1 起步 2. 获取基址 2.1 用CE查看个人信息 2.1.1 获取昵称基址 2.1.2 dll模块基址 2.2 用OD查看个人信息 2.3 内 ...

  3. Frida—HOOK 学习笔记2

    Android部分 基础知识 1)安卓分层 简单提一下安卓分层,这个点知道了更好,不知道也无所谓.毕竟我们不是开发,只是为了避免下述情况: 我要学习so文件HOOK.一波百度 "HOOK s ...

  4. Frida—HOOK 学习笔记1

    JAVA层HOOK 在开始之前,依旧是熟悉的工具官方文档.因本人英文水平不高,大佬们可通过官网(https://frida.re/docs/home/)或看雪(https://bbs.pediy.co ...

  5. Angular 依赖注入学习笔记之工厂函数的用法

    网址:https://angular.institute/di We can transfer any data through our apps, transform it and replace ...

  6. Angular input decorator学习笔记

    https://angular.io/api/core/Input Input decorator只能用在Angular class字段里, 用于指定元数据,将class字段指定成input prop ...

  7. Angular数据绑定的学习笔记

    https://angular.io/guide/property-binding Property binding in Angular helps you set values for prope ...

  8. 2020国庆节 Angular structual 指令学习笔记(<ng-template>) 包含语法糖解糖过程

    Structural directives are responsible for HTML layout. They shape or reshape the DOM's structure, ty ...

  9. angular基本知识学习笔记 - Component的基本概念

    官网地址:https://angular.io/guide/architecture Angular is a platform and framework for building single-p ...

最新文章

  1. C++知识点36——使用智能指针的注意事项(下)
  2. 【工作感悟】mysql授权root远程访问
  3. 资深数据产品经理陈家崑:如何从 0 到 1 构建埋点体系
  4. Android之让EditText不被键盘挡住
  5. 用户画像之Spark ML实现
  6. zookeeper在Windows底下的安装和使用
  7. 【报告分享】字节跳动2019年企业社会责任报告.pdf(附下载链接)
  8. 软件开发实践的24条军规
  9. Android入门教程 (零)付费专栏课程规划
  10. matlab qam调制函数,matlab实现16QAM调制解调
  11. python-opencv最大内切圆查找
  12. 数据库——T-SQL方式创建数据库
  13. 服务器80端口找不到,如何查询服务器80端口被关闭
  14. 苹果照片未删却不见了_手机删除的照片如何恢复?不得不说这方法好!
  15. 老男孩网络安全 | 从小白到月薪13k,仅仅只用了三个多月的时间!
  16. Vue3+Typescript+Vite实现网易云音乐年活动主导色
  17. java计算机毕业设计在线影视系统源码+数据库+lw文档+系统+部署
  18. 光伏产业红与黑 需求寒冬背后暗藏分布式春天
  19. CAD梦想画图的浏览模式与绘图模式
  20. C++题解:蜗牛旅行

热门文章

  1. 「最简单」的 Core Data 上手指南
  2. Javascript - ExtJs - TreePanel组件
  3. 在Centos 7中开放80端口
  4. 产品经理,如何建立自己独特的产品观?(转)
  5. 开源学习管理系统(LMS)的比较
  6. 财务一体化项目,进度与计划11
  7. [转载]----linux系统工程师的前途在哪里?
  8. JAVA设计模式总结之23种设计模式
  9. Virtual Box 安装过程(卸载Vmware后)
  10. 异常-throws的方式处理异常