# Lifecycle Hooks
# 生命周期钩子
A component has a lifecycle managed by Angular .
每个组件都有一个被Angular管理的生命周期。
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创建它,渲染它,创建并渲染它的子组件,在它被绑定的属性发生变化时检查它,并在它从DOM中被移除前销毁它。
Angular offers **lifecycle hooks**
that provide visibility into these key life moments and the ability to act when they occur.
Angular提供了**生命周期钩子**,把这些关键生命时刻暴露出来,赋予我们在它们发生时采取行动的能力。
A directive has the same set of lifecycle hooks, minus the hooks that are specific to component content and views.
除了那些组件内容和视图相关的钩子外,指令有相同生命周期钩子。
{@a hooks-overview}
## Component lifecycle hooks overview
## 组件生命周期钩子概览
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.
指令和组件的实例有一个生命周期:新建、更新和销毁。
通过实现一个或多个Angular `core`库里定义的*生命周期钩子*接口,开发者可以介入该生命周期中的这些关键时刻。
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:
每个接口都有唯一的一个钩子方法,它们的名字是由接口名再加上`ng`前缀构成的。比如,`OnInit`接口的钩子方法叫做`ngOnInit`,
Angular在创建组件后立刻调用它,:
Hook 钩子 |
Purpose and Timing 目的和时机 |
---|---|
ngOnChanges()
|
Respond when Angular (re)sets data-bound input properties. The method receives a `SimpleChanges` object of current and previous property values. 当Angular(重新)设置数据绑定输入属性时响应。 该方法接受当前和上一属性值的`SimpleChanges`对象 Called before `ngOnInit()` and whenever one or more data-bound input properties change. 当被绑定的输入属性的值发生变化时调用,首次调用一定会发生在`ngOnInit()`之前。 |
ngOnInit()
|
Initialize the directive/component after Angular first displays the data-bound properties and sets the directive/component's input properties. 在Angular第一次显示数据绑定和设置指令/组件的输入属性之后,初始化指令/组件。 Called _once_, after the _first_ `ngOnChanges()`. 在第一轮`ngOnChanges()`完成之后调用,只调用**一次**。 |
ngDoCheck()
|
Detect and act upon changes that Angular can't or won't detect on its own. 检测,并在发生Angular无法或不愿意自己检测的变化时作出反应。 Called during every change detection run, immediately after `ngOnChanges()` and `ngOnInit()`. 在每个Angular变更检测周期中调用,`ngOnChanges()`和`ngOnInit()`之后。 |
ngAfterContentInit()
|
Respond after Angular projects external content into the component's view. 当把内容投影进组件之后调用。 Called _once_ after the first `ngDoCheck()`. 第一次`ngDoCheck()`之后调用,只调用一次。 _A component-only hook_. **只适用于组件**。 |
ngAfterContentChecked()
|
Respond after Angular checks the content projected into the component. 每次完成被投影组件内容的变更检测之后调用。 Called after the `ngAfterContentInit()` and every subsequent `ngDoCheck()`. `ngAfterContentInit()`和每次`ngDoCheck()`之后调用 _A component-only hook_. **只适合组件**。 |
ngAfterViewInit()
|
Respond after Angular initializes the component's views and child views. 初始化完组件视图及其子视图之后调用。 Called _once_ after the first `ngAfterContentChecked()`. 第一次`ngAfterContentChecked()`之后调用,只调用一次。 _A component-only hook_. **只适合组件**。 |
ngAfterViewChecked()
|
Respond after Angular checks the component's views and child views. 每次做完组件视图和子视图的变更检测之后调用。 Called after the `ngAfterViewInit` and every subsequent `ngAfterContentChecked()`. `ngAfterViewInit()`和每次`ngAfterContentChecked()`之后调用。 _A component-only hook_. **只适合组件**。 |
ngOnDestroy
|
Cleanup just before Angular destroys the directive/component. Unsubscribe Observables and detach event handlers to avoid memory leaks. 当Angular每次销毁指令/组件之前调用并清扫。 在这儿反订阅可观察对象和分离事件处理器,以防内存泄漏。 Called _just before_ Angular destroys the directive/component. 在Angular销毁指令/组件之前调用。 |
Component 组件 |
Description 描述 |
---|---|
Peek-a-boo | Demonstrates every lifecycle hook. Each hook method writes to the on-screen log. 展示每个生命周期钩子,每个钩子方法都会在屏幕上显示一条日志。 |
Spy |
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.
指令也同样有生命周期钩子。我们新建了一个`SpyDirective`,利用`ngOnInit`和`ngOnDestroy`钩子,在它所监视的每个元素被创建或销毁时输出日志。
This example applies the `SpyDirective` to a ` ` in an `ngFor` *hero* repeater
managed by the parent `SpyComponent`.
本例把`SpyDirective`应用到父组件里的`ngFor`*英雄*重复器(repeater)的` `里面。
|
OnChanges | 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. 这里将会看到:每当组件的输入属性发生变化时,Angular会如何以`changes`对象作为参数去调用`ngOnChanges()`钩子。 展示了该如何理解和使用`changes`对象。 |
DoCheck | Implements an `ngDoCheck()` method with custom change detection. See how often Angular calls this hook and watch it post changes to a log. 实现了一个`ngDoCheck()`方法,通过它可以自定义变更检测逻辑。 这里将会看到:Angular会用什么频度调用这个钩子,监视它的变化,并把这些变化输出成一条日志。 |
AfterView | Shows what Angular means by a *view*. Demonstrates the `ngAfterViewInit` and `ngAfterViewChecked` hooks. 显示Angular中的*视图*所指的是什么。 演示了`ngAfterViewInit`和`ngAfterViewChecked`钩子。 |
AfterContent | 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. 展示如何把外部内容投影进组件中,以及如何区分“投影进来的内容”和“组件的子视图”。 演示了`ngAfterContentInit`和`ngAfterContentChecked`钩子。 |
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. 在这个例子中,每当父组件递增它的输入属性`counter`时,`CounterComponent`就会通过`ngOnChanges`记录一条变更。 同时,我们还把前一个例子中的`SpyDirective`用在`CounterComponent`上,来提供日志,可以同时观察到日志的创建和销毁过程。 |