You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The peek-a-boo example image does not exactly reflect what happens. This updates the hook table and the image of the peek-a-boo output. Closesangular#40138 PR Closeangular#47219
Copy file name to clipboardExpand all lines: aio/content/guide/lifecycle-hooks.md
+17-13Lines changed: 17 additions & 13 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -40,7 +40,7 @@ Use them to perform the following kinds of operations.
40
40
41
41
| Hook method | Purpose | Timing |
42
42
|:--- |:--- |:--- |
43
-
|`ngOnChanges()`| Respond when Angular sets or resets data-bound input properties. The method receives a `SimpleChanges` object of current and previous property values. <br /> <divclass="alert is-helpful"> **NOTE**: <br /> This happens very frequently, so any operation you perform here impacts performance significantly. </div> See details in [Using change detection hooks](#onchanges) in this document. | Called before `ngOnInit()`\(if the component has bound inputs\) and whenever one or more data-bound input properties change. <br /> <divclass="alert is-helpful"> **NOTE**: <br /> If your component has no inputs or you use it without providing any inputs, the framework will not call `ngOnChanges()`. </div> |
43
+
|`ngOnChanges()`| Respond when Angular sets or resets data-bound input properties. The method receives a `SimpleChanges` object of current and previous property values. <br /> <divclass="alert is-helpful"> **NOTE**: <br /> This happens frequently, so any operation you perform here impacts performance significantly. </div> See details in [Using change detection hooks](#onchanges) in this document. | Called before `ngOnInit()`\(if the component has bound inputs\) and whenever one or more data-bound input properties change. <br /> <divclass="alert is-helpful"> **NOTE**: <br /> If your component has no inputs or you use it without providing any inputs, the framework will not call `ngOnChanges()`. </div> |
44
44
|`ngOnInit()`| Initialize the directive or component after Angular first displays the data-bound properties and sets the directive or component's input properties. See details in [Initializing a component or directive](#oninit) in this document. | Called once, after the first `ngOnChanges()`. `ngOnInit()` is still called even when `ngOnChanges()` is not \(which is the case when there are no template-bound inputs\). |
45
45
|`ngDoCheck()`| Detect and act upon changes that Angular can't or won't detect on its own. See details and example in [Defining custom change detection](#docheck) in this document. | Called immediately after `ngOnChanges()` on every change detection run, and immediately after `ngOnInit()` on the first run. |
46
46
|`ngAfterContentInit()`| Respond after Angular projects external content into the component's view, or into the view that a directive is in. <br /> See details and example in [Responding to changes in content](#aftercontent) in this document. | Called *once* after the first `ngDoCheck()`. |
@@ -121,12 +121,16 @@ The sequence of log messages follows the prescribed hook calling order:
121
121
|:--- |:--- |
122
122
| 1 |`OnChanges`|
123
123
| 2 |`OnInit`|
124
-
| 3-5 |`DoCheck`|
125
-
| 6 |`AfterContentInit`|
126
-
| 7-9 |`AfterContentChecked`|
127
-
| 10 |`AfterViewInit`|
128
-
| 11-13 |`AfterViewChecked`|
129
-
| 14 |`OnDestroy`|
124
+
| 3 |`DoCheck`|
125
+
| 4 |`AfterContentInit`|
126
+
| 5 |`AfterContentChecked`|
127
+
| 6 |`AfterViewInit`|
128
+
| 7 |`AfterViewChecked`|
129
+
| 8 |`DoCheck`|
130
+
| 9 |`AfterContentChecked`|
131
+
| 10 |`AfterViewChecked`|
132
+
| 11 |`OnDestroy`|
133
+
130
134
131
135
<divclass="alert is-helpful">
132
136
@@ -151,8 +155,8 @@ The example does not perform any initialization or clean-up.
151
155
It just tracks the appearance and disappearance of an element in the view by recording when the directive itself is instantiated and destroyed.
152
156
153
157
A spy directive like this can provide insight into a DOM object that you cannot change directly.
154
-
You can't touch the implementation of a built-in `<div>`, or modify a third party component.
155
-
You can, however watch these elements with a directive.
158
+
You can't access the implementation of a built-in `<div>`, or modify a third party component.
159
+
You do have the option to watch these elements with a directive.
156
160
157
161
The directive defines `ngOnInit()` and `ngOnDestroy()` hooks
158
162
that log messages to the parent using an injected `LoggerService`.
@@ -253,8 +257,8 @@ The `LoggerService.tick_then()` statement postpones the log update for one turn
253
257
254
258
#### Write lean hook methods to avoid performance problems
255
259
256
-
When you run the *AfterView* sample, notice how frequently Angular calls `AfterViewChecked()`-often when there are no changes of interest.
257
-
Be very careful about how much logic or computation you put into one of these methods.
260
+
When you run the *AfterView* sample, notice how frequently Angular calls `AfterViewChecked()` - often when there are no changes of interest.
261
+
Be careful about how much logic or computation you put into one of these methods.
258
262
259
263
<divclass="lightbox">
260
264
@@ -284,7 +288,7 @@ AngularJS developers know this technique as *transclusion*.
284
288
The *AfterContent* sample explores the `AfterContentInit()` and `AfterContentChecked()` hooks that Angular calls *after* Angular projects external content into the component.
285
289
286
290
Consider this variation on the [previous *AfterView*](#afterview) example.
287
-
This time, instead of including the child view within the template, it imports the content from the `AfterContentComponent`'s parent.
291
+
This time, instead of including the child view within the template, it imports the content from the `AfterContentComponent` hook's parent.
0 commit comments