SlideShare a Scribd company logo
Component lifecycle hooks in Angular 2.0
Component lifecycle hooks in Angular 2.0
DOMData
<!-- Expressions -->
Please type your name : {{name}}
<!-- Directives & Data Binding -->
Name: <input ng-model="name" value="..." />
Template
name :
Scope
value
elm.bind('keydown', … )
$scope.$watch('name', … )
Directive
$rootScope
Component lifecycle hooks in Angular 2.0
Component lifecycle hooks in Angular 2.0
Component lifecycle hooks in Angular 2.0
tick(): void {
if (this._runningTick) {
throw new BaseException("ApplicationRef.tick is called recursively");
}
var s = ApplicationRef_._tickScope();
try {
this._runningTick = true;
this._changeDetectorRefs.forEach((detector) => detector.detectChanges());
if (this._enforceNoNewChanges) {
this._changeDetectorRefs
.forEach((detector) => detector.checkNoChanges());
}
} finally {
this._runningTick = false;
wtfLeave(s);
}
}
Component lifecycle hooks in Angular 2.0
Component lifecycle hooks in Angular 2.0
Component lifecycle hooks in Angular 2.0
Component lifecycle hooks in Angular 2.0
 CheckAlways
 Default
 CheckOnce
 OnPush
 Checked
 Detached
constructor(private ref: ChangeDetectorRef) {
ref.detach();
setInterval(() => {
this.ref.detectChanges();
}, 5000);
}
Doing a local check
every five seconds.
class Component{
data:any = {};
_differ: KeyValueDiffer;
constructor(private _differs:KeyValueDiffers){
this._differ = this._differs.find(this.data).create(null);
}
ngDoCheck() {
var changes = this._differ.diff(this.data);
if (changes) {
changes.forEachAddedItem ((r) => { log(r.key,r.currentValue); });
changes.forEachChangedItem((r) => { log(r.key,r.currentValue); });
changes.forEachRemovedItem((r) => { log(r.key,r.currentValue); });
}
}
}
class Component{
data:any = [];
_differ: IterableDiffer;
constructor(private _differs:IterableDiffers){
this._differ = this._differs.find(this.data).create(null);
}
ngDoCheck() {
var changes = this._differ.diff(this.data);
if (changes) {
changes.forEachAddedItem ((r) => { this.addData.push(r)});
changes.forEachRemovedItem((r) => { this.removeData.push(r)});
}
}
}
Component lifecycle hooks in Angular 2.0
Component lifecycle hooks in Angular 2.0
Component Directive
Template
Content
DOM Element
<component1 directive1>
<component2>
This is content.
</component2>
</component1>
(component1)
(component2)
(directive1)
App
Component1
Component2
Directive1
Component3
loading
<app>Loading</app>
<div>
<component1 directive1="{{name}}">
<component2>
This is content.
</component2>
</component1>
</div>
<div>
<h4>{{name}}</h4>
<ng-content />
</div>
This is
content
<div>
<h4>{{name}}</h4>
<ng-content />
</div>
Component lifecycle hooks in Angular 2.0
Component
TemplateContent
Component
TemplateContent
1
2
3
4
5
6
7
Hooks Descriptions
ngOnChanges Called when an input or output binding value changes
ngOnInit After the first ngOnChanges
ngDoCheck Developer's custom change detection
ngAfterContentInit After component content initialized
ngAfterContentChecked After every check of component content
ngAfterViewInit After component's view(s) are initialized
ngAfterViewChecked After every check of a component's view(s)
ngOnDestroy Just before the directive is destroyed
Component
Directive
TemplateContent
DOM Element
ngAfterContentInit
ngAfterContentChecked
ngAfterContentInit
ngAfterContentChecked
ngAfterContentInit
ngAfterContentChecked
ngAfterContentInit
ngAfterContentChecked
AfterViewInit
AfterViewChecked
AfterViewInit
AfterViewChecked
AfterViewInit
AfterViewChecked
AfterViewInit
AfterViewChecked
Component
Directive
TemplateContent
DOM Element
ngAfterContentChecked
ngAfterContentChecked
ngAfterContentChecked
ngAfterContentChecked
AfterViewCheckedAfterViewChecked
AfterViewChecked
AfterViewChecked
Component lifecycle hooks in Angular 2.0

More Related Content

What's hot (20)

PPTX
Angular Directives
Malla Reddy University
 
PDF
Angular Directives | Angular 2 Custom Directives | Angular Tutorial | Angular...
Edureka!
 
PDF
Angular - Chapter 7 - HTTP Services
WebStackAcademy
 
PPTX
Angular 2.0 forms
Eyal Vardi
 
PPTX
Angular Components.pptx
AshokKumar616995
 
PDF
Angular - Chapter 1 - Introduction
WebStackAcademy
 
ODP
Routing & Navigating Pages in Angular 2
Knoldus Inc.
 
PPTX
jQuery
Dileep Mishra
 
PDF
Angular Dependency Injection
Nir Kaufman
 
PPTX
Angular Data Binding
Duy Khanh
 
PDF
Fundamental JavaScript [UTC, March 2014]
Aaron Gustafson
 
PPT
Angular 8
Sunil OS
 
PPT
Introduction to Javascript
Amit Tyagi
 
PDF
Basics of React Hooks.pptx.pdf
Knoldus Inc.
 
PPTX
Angular Lifecycle Hooks
Squash Apps Pvt Ltd
 
PDF
Angular Advanced Routing
Laurent Duveau
 
PDF
Spring annotation
Rajiv Srivastava
 
PDF
Angular components
Sultan Ahmed
 
PPTX
Spring 3.x - Spring MVC - Advanced topics
Guy Nir
 
PDF
Introduzione ad angular 7/8
Valerio Radice
 
Angular Directives
Malla Reddy University
 
Angular Directives | Angular 2 Custom Directives | Angular Tutorial | Angular...
Edureka!
 
Angular - Chapter 7 - HTTP Services
WebStackAcademy
 
Angular 2.0 forms
Eyal Vardi
 
Angular Components.pptx
AshokKumar616995
 
Angular - Chapter 1 - Introduction
WebStackAcademy
 
Routing & Navigating Pages in Angular 2
Knoldus Inc.
 
Angular Dependency Injection
Nir Kaufman
 
Angular Data Binding
Duy Khanh
 
Fundamental JavaScript [UTC, March 2014]
Aaron Gustafson
 
Angular 8
Sunil OS
 
Introduction to Javascript
Amit Tyagi
 
Basics of React Hooks.pptx.pdf
Knoldus Inc.
 
Angular Lifecycle Hooks
Squash Apps Pvt Ltd
 
Angular Advanced Routing
Laurent Duveau
 
Spring annotation
Rajiv Srivastava
 
Angular components
Sultan Ahmed
 
Spring 3.x - Spring MVC - Advanced topics
Guy Nir
 
Introduzione ad angular 7/8
Valerio Radice
 

Viewers also liked (20)

PPTX
Template syntax in Angular 2.0
Eyal Vardi
 
PPTX
Angular 2 - Ahead of-time Compilation
Eyal Vardi
 
PPTX
Angular 2.0 Dependency injection
Eyal Vardi
 
PPTX
Angular 2 Architecture (Bucharest 26/10/2016)
Eyal Vardi
 
PPTX
Angular 2.0 Pipes
Eyal Vardi
 
PPTX
Http Communication in Angular 2.0
Eyal Vardi
 
PPTX
Async & Parallel in JavaScript
Eyal Vardi
 
PPTX
Angular 2.0 Routing and Navigation
Eyal Vardi
 
PPTX
Angular 1.x vs. Angular 2.x
Eyal Vardi
 
PPTX
Angular 2 Architecture
Eyal Vardi
 
PPTX
Making Angular2 lean and Fast
Vinci Rufus
 
PPTX
Node.js Socket.IO
Eyal Vardi
 
PPTX
Node.js File system & Streams
Eyal Vardi
 
PPTX
Node.js Event Emitter
Eyal Vardi
 
PPTX
Node js overview
Eyal Vardi
 
PPTX
Angular 2.0 Views
Eyal Vardi
 
PPTX
Modules and injector
Eyal Vardi
 
PPTX
Razor and the Art of Templating
Jess Chadwick
 
PPTX
Introducing Razor - A new view engine for ASP.NET
Shiju Varghese
 
PPTX
Views
Eyal Vardi
 
Template syntax in Angular 2.0
Eyal Vardi
 
Angular 2 - Ahead of-time Compilation
Eyal Vardi
 
Angular 2.0 Dependency injection
Eyal Vardi
 
Angular 2 Architecture (Bucharest 26/10/2016)
Eyal Vardi
 
Angular 2.0 Pipes
Eyal Vardi
 
Http Communication in Angular 2.0
Eyal Vardi
 
Async & Parallel in JavaScript
Eyal Vardi
 
Angular 2.0 Routing and Navigation
Eyal Vardi
 
Angular 1.x vs. Angular 2.x
Eyal Vardi
 
Angular 2 Architecture
Eyal Vardi
 
Making Angular2 lean and Fast
Vinci Rufus
 
Node.js Socket.IO
Eyal Vardi
 
Node.js File system & Streams
Eyal Vardi
 
Node.js Event Emitter
Eyal Vardi
 
Node js overview
Eyal Vardi
 
Angular 2.0 Views
Eyal Vardi
 
Modules and injector
Eyal Vardi
 
Razor and the Art of Templating
Jess Chadwick
 
Introducing Razor - A new view engine for ASP.NET
Shiju Varghese
 
Views
Eyal Vardi
 
Ad

More from Eyal Vardi (16)

PPTX
Why magic
Eyal Vardi
 
PPTX
Smart Contract
Eyal Vardi
 
PDF
Rachel's grandmother's recipes
Eyal Vardi
 
PPTX
Performance Optimization In Angular 2
Eyal Vardi
 
PPTX
Angular 2 NgModule
Eyal Vardi
 
PPTX
Upgrading from Angular 1.x to Angular 2.x
Eyal Vardi
 
PPTX
Routing And Navigation
Eyal Vardi
 
PPTX
Modules in ECMAScript 6.0
Eyal Vardi
 
PPTX
Proxies in ECMAScript 6.0
Eyal Vardi
 
PPTX
Iterators & Generators in ECMAScript 6.0
Eyal Vardi
 
PPTX
Symbols in ECMAScript 6.0
Eyal Vardi
 
PPTX
Objects & Classes in ECMAScript 6.0
Eyal Vardi
 
PPTX
Scope & Functions in ECMAScript 6.0
Eyal Vardi
 
PPTX
AngularJS Internal
Eyal Vardi
 
PPTX
Node.js Spplication Scaling
Eyal Vardi
 
PPTX
Node.js Express
Eyal Vardi
 
Why magic
Eyal Vardi
 
Smart Contract
Eyal Vardi
 
Rachel's grandmother's recipes
Eyal Vardi
 
Performance Optimization In Angular 2
Eyal Vardi
 
Angular 2 NgModule
Eyal Vardi
 
Upgrading from Angular 1.x to Angular 2.x
Eyal Vardi
 
Routing And Navigation
Eyal Vardi
 
Modules in ECMAScript 6.0
Eyal Vardi
 
Proxies in ECMAScript 6.0
Eyal Vardi
 
Iterators & Generators in ECMAScript 6.0
Eyal Vardi
 
Symbols in ECMAScript 6.0
Eyal Vardi
 
Objects & Classes in ECMAScript 6.0
Eyal Vardi
 
Scope & Functions in ECMAScript 6.0
Eyal Vardi
 
AngularJS Internal
Eyal Vardi
 
Node.js Spplication Scaling
Eyal Vardi
 
Node.js Express
Eyal Vardi
 
Ad

Recently uploaded (20)

PDF
HiHelloHR – Simplify HR Operations for Modern Workplaces
HiHelloHR
 
PPTX
Human Resources Information System (HRIS)
Amity University, Patna
 
PDF
vMix Pro 28.0.0.42 Download vMix Registration key Bundle
kulindacore
 
PPTX
Equipment Management Software BIS Safety UK.pptx
BIS Safety Software
 
PDF
Linux Certificate of Completion - LabEx Certificate
VICTOR MAESTRE RAMIREZ
 
PPTX
Why Businesses Are Switching to Open Source Alternatives to Crystal Reports.pptx
Varsha Nayak
 
PPTX
Platform for Enterprise Solution - Java EE5
abhishekoza1981
 
PPTX
Migrating Millions of Users with Debezium, Apache Kafka, and an Acyclic Synch...
MD Sayem Ahmed
 
PPTX
A Complete Guide to Salesforce SMS Integrations Build Scalable Messaging With...
360 SMS APP
 
PDF
Automate Cybersecurity Tasks with Python
VICTOR MAESTRE RAMIREZ
 
PDF
Beyond Binaries: Understanding Diversity and Allyship in a Global Workplace -...
Imma Valls Bernaus
 
PPTX
Feb 2021 Cohesity first pitch presentation.pptx
enginsayin1
 
PDF
Revenue streams of the Wazirx clone script.pdf
aaronjeffray
 
PDF
Capcut Pro Crack For PC Latest Version {Fully Unlocked} 2025
hashhshs786
 
PDF
Digger Solo: Semantic search and maps for your local files
seanpedersen96
 
PDF
Odoo CRM vs Zoho CRM: Honest Comparison 2025
Odiware Technologies Private Limited
 
PDF
Mobile CMMS Solutions Empowering the Frontline Workforce
CryotosCMMSSoftware
 
PDF
Understanding the Need for Systemic Change in Open Source Through Intersectio...
Imma Valls Bernaus
 
PDF
iTop VPN With Crack Lifetime Activation Key-CODE
utfefguu
 
PDF
Alexander Marshalov - How to use AI Assistants with your Monitoring system Q2...
VictoriaMetrics
 
HiHelloHR – Simplify HR Operations for Modern Workplaces
HiHelloHR
 
Human Resources Information System (HRIS)
Amity University, Patna
 
vMix Pro 28.0.0.42 Download vMix Registration key Bundle
kulindacore
 
Equipment Management Software BIS Safety UK.pptx
BIS Safety Software
 
Linux Certificate of Completion - LabEx Certificate
VICTOR MAESTRE RAMIREZ
 
Why Businesses Are Switching to Open Source Alternatives to Crystal Reports.pptx
Varsha Nayak
 
Platform for Enterprise Solution - Java EE5
abhishekoza1981
 
Migrating Millions of Users with Debezium, Apache Kafka, and an Acyclic Synch...
MD Sayem Ahmed
 
A Complete Guide to Salesforce SMS Integrations Build Scalable Messaging With...
360 SMS APP
 
Automate Cybersecurity Tasks with Python
VICTOR MAESTRE RAMIREZ
 
Beyond Binaries: Understanding Diversity and Allyship in a Global Workplace -...
Imma Valls Bernaus
 
Feb 2021 Cohesity first pitch presentation.pptx
enginsayin1
 
Revenue streams of the Wazirx clone script.pdf
aaronjeffray
 
Capcut Pro Crack For PC Latest Version {Fully Unlocked} 2025
hashhshs786
 
Digger Solo: Semantic search and maps for your local files
seanpedersen96
 
Odoo CRM vs Zoho CRM: Honest Comparison 2025
Odiware Technologies Private Limited
 
Mobile CMMS Solutions Empowering the Frontline Workforce
CryotosCMMSSoftware
 
Understanding the Need for Systemic Change in Open Source Through Intersectio...
Imma Valls Bernaus
 
iTop VPN With Crack Lifetime Activation Key-CODE
utfefguu
 
Alexander Marshalov - How to use AI Assistants with your Monitoring system Q2...
VictoriaMetrics
 

Component lifecycle hooks in Angular 2.0