@@ -5,30 +5,37 @@ block includes
5
5
6
6
:marked
7
7
# Component Lifecycle
8
- A Component has a lifecycle managed by Angular itself. Angular creates it, renders it, creates and renders its children,
8
+ figure
9
+ img( src ="/resources/images/devguide/lifecycle-hooks/hooks-in-sequence.png" alt ="Us" align ="left" style ="width:200px; margin-left:-40px;margin-right:30px" )
10
+ :marked
11
+ A component has a lifecycle managed by Angular itself.
12
+
13
+ Angular creates it, renders it, creates and renders its children,
9
14
checks it when its data-bound properties change, and destroys it before removing it from the DOM.
10
15
11
- Angular offers **component lifecycle hooks**
12
- that provide visibility into these key moments and the ability to act when they occur.
16
+ Angular offers **lifecycle hooks**
17
+ that provide visibility into these key life moments and the ability to act when they occur.
13
18
19
+ A directive has the same set of lifecycle hooks, minus the hooks that are specific to component content and views.
20
+ <br clear="all">
14
21
## Table of Contents
15
- * [The lifecycle hooks ](#hooks-overview)
16
- * [_What_ they are](#hook-descriptions)
17
- * [_When_ they are called ](#hook-sequence )
22
+ * [Overview ](#hooks-overview)
23
+ <br><br>
24
+ * [Each hook's purpose and timing ](#hooks-purpose-timing )
18
25
+ ifDocsFor('ts|js' )
19
26
:marked
20
27
* [Interfaces are optional (technically)](#interface-optional)
28
+ :marked
29
+ * [Other Angular lifecycle hooks](#other-lifecycle-hooks)
30
+ <br><br>
31
+ * [The lifecycle sample](#the-sample)
32
+ * [All](#peek-a-boo)
33
+ * [Spying OnInit and OnDestroy](#spy)
34
+ * [OnChanges](#onchanges)
35
+ * [DoCheck](#docheck)
36
+ * [AfterViewInit and AfterViewChecked](#afterview)
37
+ * [AfterContentInit and AfterContentChecked](#aftercontent)
21
38
:marked
22
- * [Other Angular lifecycle hooks](#other-lifecycle-hooks)
23
- <br><br>
24
- * [The lifecycle sample](#the-sample)
25
- * [All](#peek-a-boo)
26
- * [Spying OnInit and OnDestroy](#spy)
27
- * [OnChanges](#onchanges)
28
- * [DoCheck](#docheck)
29
- * [AfterViewInit and AfterViewChecked](#afterview)
30
- * [AfterContentInit and AfterContentChecked](#aftercontent)
31
-
32
39
Try the <live-example></live-example>.
33
40
34
41
a#hooks-overview
@@ -48,75 +55,7 @@ a#hooks-overview
48
55
No directive or component will implement all of the lifecycle hooks and some of the hooks only make sense for components.
49
56
Angular only calls a directive/component hook method *if it is defined*.
50
57
51
- a#hook-descriptions
52
- :marked
53
- Here are descriptions of the component lifecycle hook methods followed by a chart describing [when they are called](#hook-sequence):
54
-
55
- ### Directives and Components
56
-
57
- table( width ="100%" )
58
- col( width ="20%" )
59
- col( width ="80%" )
60
- tr
61
- th Hook
62
- th Purpose
63
- tr( style =top)
64
- td ngOnInit
65
- td
66
- :marked
67
- Initialize the directive/component after Angular initializes the data-bound input properties.
68
- tr( style =top)
69
- td ngOnChanges
70
- td
71
- :marked
72
- Respond after Angular sets a data-bound input property.
73
- The method receives a `changes` object of current and previous values.
74
- tr( style =top)
75
- td ngDoCheck
76
- td
77
- :marked
78
- Detect and act upon changes that Angular can't or won't
79
- detect on its own. Called every change detection run.
80
- tr( style =top)
81
- td ngOnDestroy
82
- td
83
- :marked
84
- Cleanup just before Angular destroys the directive/component.
85
- Unsubscribe observables and detach event handlers to avoid memory leaks.
86
-
87
- :marked
88
- ### Components only
89
-
90
- table( width ="100%" )
91
- col( width ="20%" )
92
- col( width ="80%" )
93
- tr
94
- th Hook
95
- th Purpose
96
- tr( style =top)
97
- td ngAfterContentInit
98
- td
99
- :marked
100
- After Angular projects external content into its view.
101
- tr( style =top)
102
- td ngAfterContentChecked
103
- td
104
- :marked
105
- After Angular checks the bindings of the external content that it projected into its view.
106
- tr( style =top)
107
- td ngAfterViewInit
108
- td
109
- :marked
110
- After Angular creates the component's view(s).
111
- tr( style =top)
112
- td ngAfterViewChecked
113
- td
114
- :marked
115
- After Angular checks the bindings of the component's view(s).
116
- :marked
117
- Angular does not call the hook methods in this order.
118
-
119
- a#hook-sequence
58
+ a#hooks-purpose-timing
120
59
.l-main-section
121
60
:marked
122
61
## Lifecycle sequence
@@ -127,47 +66,82 @@ table(width="100%")
127
66
col( width ="80%" )
128
67
tr
129
68
th Hook
130
- th Timing
69
+ th Purpose and Timing
70
+
131
71
tr( style =top)
132
72
td ngOnChanges
133
73
td
134
74
:marked
135
- before `ngOnInit` and when a data-bound input property value changes.
75
+ Respond when Angular (re)sets data-bound input properties.
76
+ The method receives a `SimpleChanges` object of current and previous property values.
77
+
78
+ Called before `ngOnInit` and whenever one or more data-bound input properties change.
79
+
136
80
tr( style =top)
137
81
td ngOnInit
138
82
td
139
83
:marked
140
- after the first `ngOnChanges`.
84
+ Initialize the directive/component after Angular first displays the data-bound properties
85
+ and sets the directive/component's input properties.
86
+
87
+ Called _once_, after the _first_ `ngOnChanges`.
88
+
141
89
tr( style =top)
142
90
td ngDoCheck
143
91
td
144
92
:marked
145
- during every Angular change detection cycle.
93
+ Detect and act upon changes that Angular can't or won't detect on its own.
94
+
95
+ Called during every change detection run, immediately after `ngOnChanges` and `ngOnInit`.
96
+
146
97
tr( style =top)
147
98
td ngAfterContentInit
148
99
td
149
100
:marked
150
- after projecting content into the component.
101
+ Respond after Angular projects external content into the component's view.
102
+
103
+ Called _once_ after the first `NgDoCheck`.
104
+
105
+ _A component-only hook_.
106
+
151
107
tr( style =top)
152
108
td ngAfterContentChecked
153
109
td
154
110
:marked
155
- after every check of projected component content.
111
+ Respond after Angular checks the content projected into the component.
112
+
113
+ Called after the `ngAfterContentInit` and every subsequent `NgDoCheck`.
114
+
115
+ _A component-only hook_.
116
+
156
117
tr( style =top)
157
118
td ngAfterViewInit
158
119
td
159
120
:marked
160
- after initializing the component's views and child views.
121
+ Respond after Angular initializes the component's views and child views.
122
+
123
+ Called _once_ after the first `ngAfterContentChecked`.
124
+
125
+ _A component-only hook_.
126
+
161
127
tr( style =top)
162
128
td ngAfterViewChecked
163
129
td
164
130
:marked
165
- after every check of the component's views and child views.
131
+ Respond after Angular checks the component's views and child views.
132
+
133
+ Called after the `ngAfterViewInit` and every subsequent `ngAfterContentChecked`.
134
+
135
+ _A component-only hook_.
136
+
166
137
tr( style =top)
167
138
td ngOnDestroy
168
139
td
169
140
:marked
170
- just before Angular destroys the directive/component.
141
+ Cleanup just before Angular destroys the directive/component.
142
+ Unsubscribe observables and detach event handlers to avoid memory leaks.
143
+
144
+ Called _just before_ Angular destroys the directive/component.
171
145
172
146
+ ifDocsFor('ts|js' )
173
147
a#interface-optional
0 commit comments