Angular creates it, renders it, creates and renders its children,
checks it when its data-bound properties change, and destroys it before removing it from the DOM.
Angular提供了生命周期钩子,把这些关键生命时刻暴露出来,赋予我们在它们发生时采取行动的能力。
Angular offers lifecycle hooks
that provide visibility into these key life moments and the ability to act when they occur.
除了那些组件内容和视图相关的钩子外,指令有相同生命周期钩子。
A directive has the same set of lifecycle hooks, minus the hooks that are specific to component content and views.
Directive and component instances have a lifecycle
as Angular creates, updates, and destroys them.
Developers can tap into key moments in that lifecycle by implementing
one or more of the lifecycle hook interfaces in the Angular core library.
Each interface has a single hook method whose name is the interface name prefixed with ng.
For example, the OnInit interface has a hook method named ngOnInit()
that Angular calls shortly after creating the component:
No directive or component will implement all of the lifecycle hooks and some of the hooks only make sense for components.
Angular only calls a directive/component hook method if it is defined.
After creating a component/directive by calling its constructor, Angular
calls the lifecycle hook methods in the following sequence at specific moments:
The interfaces are optional for JavaScript and Typescript developers from a purely technical perspective.
The JavaScript language doesn't have interfaces.
Angular can't see TypeScript interfaces at runtime because they disappear from the transpiled JavaScript.
幸运的是,它们并不是必须的。
Fortunately, they aren't necessary.
我们并不需要在指令和组件上添加生命周期钩子接口就能获得钩子带来的好处。
You don't have to add the lifecycle hook interfaces to directives and components to benefit from the hooks themselves.
Angular instead inspects directive and component classes and calls the hook methods if they are defined.
Angular finds and calls methods like ngOnInit(), with or without the interfaces.
The live example / downloadable example
demonstrates the lifecycle hooks in action through a series of exercises
presented as components under the control of the root AppComponent.
它们遵循了一个常用的模式:用子组件演示一个或多个生命周期钩子方法,而父组件被当作该子组件的测试台。
They follow a common pattern: a parent component serves as a test rig for
a child component that illustrates one or more of the lifecycle hook methods.
Directives have lifecycle hooks too.
A SpyDirective can log when the element it spies upon is
created or destroyed using the ngOnInit and ngOnDestroy hooks.
See how Angular calls the ngOnChanges() hook with a changes object
every time one of the component input properties changes.
Shows how to interpret the changes object.
Shows how to project external content into a component and
how to distinguish projected content from a component's view children.
Demonstrates the ngAfterContentInit and ngAfterContentChecked hooks.
计数器
Counter
演示了组件和指令的组合,它们各自有自己的钩子。
Demonstrates a combination of a component and a directive
each with its own hooks.
In this example, a CounterComponent logs a change (via ngOnChanges)
every time the parent component increments its input counter property.
Meanwhile, the SpyDirective from the previous example is applied
to the CounterComponent log where it watches log entries being created and destroyed.
接下来,我们将详细讨论这些练习。
The remainder of this page discusses selected exercises in further detail.
Peek-a-boo:全部钩子
Peek-a-boo: all hooks
PeekABooComponent组件演示了组件中所有可能存在的钩子。
The PeekABooComponent demonstrates all of the hooks in one component.
The constructor isn't an Angular hook per se.
The log confirms that input properties (the name property in this case) have no assigned values at construction.
Had the user clicked the Update Hero button, the log would show another OnChanges and two more triplets of
DoCheck, AfterContentChecked and AfterViewChecked.
Clearly these three hooks fire often. Keep the logic in these hooks as lean as possible!
下一个例子就聚焦于这些钩子的细节上。
The next examples focus on hook details.
窥探OnInit和OnDestroy
Spying OnInit and OnDestroy
潜入这两个spy钩子来发现一个元素是什么时候被初始化或者销毁的。
Go undercover with these two spy hooks to discover when an element is initialized or destroyed.
指令是一种完美的渗透方式,我们的英雄永远不会知道该指令的存在。
This is the perfect infiltration job for a directive.
The heroes will never know they're being watched.
不开玩笑了,注意下面两个关键点:
Kidding aside, pay attention to two key points:
就像对组件一样,Angular也会对指令调用这些钩子方法。
Angular calls hook methods for directives as well as components.
A spy directive can provide insight into a DOM object that you cannot change directly.
Obviously you can't touch the implementation of a native <div>.
You can't modify a third party component either.
But you can watch both with a directive.
The sneaky spy directive is simple, consisting almost entirely of ngOnInit() and ngOnDestroy() hooks
that log messages to the parent via an injected LoggerService.
// Spy on any element to which it is applied.// Usage: <div mySpy>...</div>@Directive({selector:'[mySpy]'})exportclassSpyDirectiveimplementsOnInit,OnDestroy{
constructor(private logger:LoggerService){}
ngOnInit(){this.logIt(`onInit`);}
ngOnDestroy(){this.logIt(`onDestroy`);}private logIt(msg:string){this.logger.log(`Spy #${nextId++} ${msg}`);}}
You can apply the spy to any native or component element and it'll be initialized and destroyed
at the same time as that element.
Here it is attached to the repeated hero <div>:
<div *ngFor="let hero of heroes"mySpyclass="heroes">
{{hero}}
</div>
The Reset button clears the heroes list.
Angular removes all hero <div> elements from the DOM and destroys their spy directives at the same time.
The spy's ngOnDestroy() method reports its last moments.
在真实的应用程序中,ngOnInit()和ngOnDestroy()方法扮演着更重要的角色。
The ngOnInit() and ngOnDestroy() methods have more vital roles to play in real applications.
OnInit()
使用ngOnInit()有两个原因:
Use ngOnInit() for two main reasons:
在构造函数之后马上执行复杂的初始化逻辑
To perform complex initializations shortly after construction.
在Angular设置完输入属性之后,对该组件进行准备。
To set up the component after Angular sets the input properties.
有经验的开发者认同组件的构建应该很便宜和安全。
Experienced developers agree that components should be cheap and safe to construct.
Don't fetch data in a component constructor.
You shouldn't worry that a new component will try to contact a remote server when
created under test or before you decide to display it.
Constructors should do no more than set the initial local variables to simple values.
Remember also that a directive's data-bound input properties are not set until after construction.
That's a problem if you need to initialize the directive based on those properties.
They'll have been set when ngOnInit() runs.
The ngOnChanges() method is your first opportunity to access those properties.
Angular calls ngOnChanges() before ngOnInit() and many times after that.
It only calls ngOnInit() once.
This is the place to free resources that won't be garbage collected automatically.
Unsubscribe from Observables and DOM events. Stop interval timers.
Unregister all callbacks that this directive registered with global or application services.
You risk memory leaks if you neglect to do so.
The ngOnChanges() method takes an object that maps each changed property name to a
SimpleChange object holding the current and previous property values.
This hook iterates over the changed properties and logs them.
这个例子中的OnChangesComponent组件有两个输入属性:hero和power。
The example component, OnChangesComponent, has two input properties: hero and power.
@Input() hero:Hero;@Input() power:string;
宿主OnChangesParentComponent绑定了它们,就像这样:
The host OnChangesParentComponent binds to them like this:
The log entries appear as the string value of the power property changes.
But the ngOnChanges does not catch changes to hero.name
That's surprising at first.
Angular only calls the hook when the value of the input property changes.
The value of the hero property is the reference to the hero object.
Angular doesn't care that the hero's own name property changed.
The hero object reference didn't change so, from Angular's perspective, there is no change to report!
DoCheck()
使用DoCheck钩子来检测那些Angular自身无法捕获的变更并采取行动。
Use the DoCheck hook to detect and act upon changes that Angular doesn't catch on its own.
用这个方法来检测那些被Angular忽略的更改。
Use this method to detect a change that Angular overlooked.
DoCheck范例通过下面的ngDoCheck()实现扩展了OnChanges范例:
The DoCheck sample extends the OnChanges sample with the following ngDoCheck() hook:
DoCheckComponent (ngDoCheck)
ngDoCheck(){if(this.hero.name !==this.oldHeroName){this.changeDetected =true;this.changeLog.push(`DoCheck: Hero name changed to "${this.hero.name}" from "${this.oldHeroName}"`);this.oldHeroName =this.hero.name;}if(this.power !==this.oldPower){this.changeDetected =true;this.changeLog.push(`DoCheck: Power changed to "${this.power}" from "${this.oldPower}"`);this.oldPower =this.power;}if(this.changeDetected){this.noChangeCount =0;}else{// log that hook was called when there was no relevant change.let count =this.noChangeCount +=1;let noChangeMsg =`DoCheck called ${count}x when no change to hero or power`;if(count ===1){// add new "no change" messagethis.changeLog.push(noChangeMsg);}else{// update last "no change" messagethis.changeLog[this.changeLog.length -1]= noChangeMsg;}}this.changeDetected =false;}
This code inspects certain values of interest, capturing and comparing their current state against previous values.
It writes a special message to the log when there are no substantive changes to the hero or the power
so you can see how often DoCheck is called. The results are illuminating:
While the ngDoCheck() hook can detect when the hero's name has changed, it has a frightful cost.
This hook is called with enormous frequency—after every
change detection cycle no matter where the change occurred.
It's called over twenty times in this example before the user can do anything.
Most of these initial checks are triggered by Angular's first rendering of unrelated data elsewhere on the page.
Mere mousing into another <input> triggers a call.
Relatively few calls reveal actual changes to pertinent data.
Clearly our implementation must be very lightweight or the user experience suffers.
The following hooks take action based on changing values within the child view,
which can only be reached by querying for the child view via the property decorated with
@ViewChild.
AfterViewComponent (class excerpts)
exportclassAfterViewComponentimplementsAfterViewChecked,AfterViewInit{private prevHero ='';// Query for a VIEW child of type `ChildViewComponent`@ViewChild(ChildViewComponent) viewChild:ChildViewComponent;
ngAfterViewInit(){// viewChild is set after the view has been initializedthis.logIt('AfterViewInit');this.doSomething();}
ngAfterViewChecked(){// viewChild is updated after the view has been checkedif(this.prevHero ===this.viewChild.hero){this.logIt('AfterViewChecked (no change)');}else{this.prevHero =this.viewChild.hero;this.logIt('AfterViewChecked');this.doSomething();}}// ...}
遵循单向数据流规则
Abide by the unidirectional data flow rule
当英雄的名字超过10个字符时,doSomething()方法就会更新屏幕。
The doSomething() method updates the screen when the hero name exceeds 10 characters.
AfterViewComponent (doSomething)
// This surrogate for real business logic sets the `comment`private doSomething(){let c =this.viewChild.hero.length >10?`That's a long name`:'';if(c !==this.comment){// Wait a tick because the component's view has already been checkedthis.logger.tick_then(()=>this.comment = c);}}
为什么在更新comment属性之前,doSomething()方法要等上一拍(tick)?
Why does the doSomething() method wait a tick before updating comment?
Angular's unidirectional data flow rule forbids updates to the view after it has been composed.
Both of these hooks fire after the component's view has been composed.
Angular throws an error if the hook updates the component's data-bound comment property immediately (try it!).
The LoggerService.tick_then() postpones the log update
for one turn of the browser's JavaScript cycle and that's just long enough.
Notice that Angular frequently calls AfterViewChecked(), often when there are no changes of interest.
Write lean hook methods to avoid performance problems.
The AfterContent sample explores the AfterContentInit() and AfterContentChecked() hooks that Angular calls
after Angular projects external content into the component.
内容投影
Content projection
内容投影是从组件外部导入HTML内容,并把它插入在组件模板中指定位置上的一种途径。
Content projection is a way to import HTML content from outside the component and insert that content
into the component's template in a designated spot.
AngularJS的开发者大概知道一项叫做transclusion的技术,对,这就是它的马甲。
AngularJS developers know this technique as transclusion.
Consider this variation on the previous AfterView example.
This time, instead of including the child view within the template, it imports the content from
the AfterContentComponent's parent. Here's the parent's template:
Notice that the <my-child> tag is tucked between the <after-content> tags.
Never put content between a component's element tags unless you intend to project that content
into the component.
The <ng-content> tag is a placeholder for the external content.
It tells Angular where to insert that content.
In this case, the projected content is the <my-child> from the parent.
下列迹象表明存在着内容投影:
The telltale signs of content projection are twofold:
在组件的元素标签中有HTML
HTML between component element tags.
组件的模板中出现了<ng-content>标签
The presence of <ng-content> tags in the component's template.
AfterContent钩子
AfterContent hooks
AfterContent钩子和AfterView相似。关键的不同点是子组件的类型不同。
AfterContent hooks are similar to the AfterView hooks.
The key difference is in the child component.
The following AfterContent hooks take action based on changing values in a content child,
which can only be reached by querying for them via the property decorated with
@ContentChild.
AfterContentComponent (class excerpts)
exportclassAfterContentComponentimplementsAfterContentChecked,AfterContentInit{private prevHero ='';
comment ='';// Query for a CONTENT child of type `ChildComponent`@ContentChild(ChildComponent) contentChild:ChildComponent;
ngAfterContentInit(){// contentChild is set after the content has been initializedthis.logIt('AfterContentInit');this.doSomething();}
ngAfterContentChecked(){// contentChild is updated after the content has been checkedif(this.prevHero ===this.contentChild.hero){this.logIt('AfterContentChecked (no change)');}else{this.prevHero =this.contentChild.hero;this.logIt('AfterContentChecked');this.doSomething();}}// ...}
Recall that Angular calls both AfterContent hooks before calling either of the AfterView hooks.
Angular completes composition of the projected content before finishing the composition of this component's view.
There is a small window between the AfterContent... and AfterView... hooks to modify the host view.