Skip to content
This repository was archived by the owner on Dec 4, 2017. It is now read-only.

Commit f45c999

Browse files
authored
docs(lifecycle-hooks): combine what and when in one chart; add img (#2469)
1 parent ac0e82c commit f45c999

File tree

2 files changed

+68
-94
lines changed

2 files changed

+68
-94
lines changed

public/docs/ts/latest/guide/lifecycle-hooks.jade

Lines changed: 68 additions & 94 deletions
Original file line numberDiff line numberDiff line change
@@ -5,30 +5,37 @@ block includes
55

66
:marked
77
# 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,
914
checks it when its data-bound properties change, and destroys it before removing it from the DOM.
1015

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.
1318

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">
1421
## 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)
1825
+ifDocsFor('ts|js')
1926
:marked
2027
* [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)
2138
: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-
3239
Try the <live-example></live-example>.
3340

3441
a#hooks-overview
@@ -48,75 +55,7 @@ a#hooks-overview
4855
No directive or component will implement all of the lifecycle hooks and some of the hooks only make sense for components.
4956
Angular only calls a directive/component hook method *if it is defined*.
5057

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
12059
.l-main-section
12160
:marked
12261
## Lifecycle sequence
@@ -127,47 +66,82 @@ table(width="100%")
12766
col(width="80%")
12867
tr
12968
th Hook
130-
th Timing
69+
th Purpose and Timing
70+
13171
tr(style=top)
13272
td ngOnChanges
13373
td
13474
: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+
13680
tr(style=top)
13781
td ngOnInit
13882
td
13983
: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+
14189
tr(style=top)
14290
td ngDoCheck
14391
td
14492
: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+
14697
tr(style=top)
14798
td ngAfterContentInit
14899
td
149100
: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+
151107
tr(style=top)
152108
td ngAfterContentChecked
153109
td
154110
: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+
156117
tr(style=top)
157118
td ngAfterViewInit
158119
td
159120
: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+
161127
tr(style=top)
162128
td ngAfterViewChecked
163129
td
164130
: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+
166137
tr(style=top)
167138
td ngOnDestroy
168139
td
169140
: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.
171145

172146
+ifDocsFor('ts|js')
173147
a#interface-optional
Loading

0 commit comments

Comments
 (0)