官术网_书友最值得收藏!

Lifecycle hooks

Angular components come with lifecycle hooks, which get executed at specific times in the component's life. For this purpose, Angular offers different interfaces. Each interface has a method of the same name as the interface name with the prefix ng. Each method is executed when the corresponding lifecycle event occurs. They are also called lifecycle hook methods. Angular calls the lifecycle hook methods in the following sequence after the constructor has been called:

The lifecycle hook method Purpose and timing ngOnChanges This is called whenever one or more data-bound input properties change. This method is called on initial changes (before ngOnInit) and any other subsequent changes. This method has one parameter--an object with keys of type string and values of type SimpleChange. The keys are the component's property names. The SimpleChange object contains current and previous values. A usage example is shown next. ngOnInit This is called once, after the first ngOnChanges. Note that the constructor of a component should only be used for dependency injection because data-bound input values are not yet set in the constructor. Everything else should be moved to the ngOnInit hook. A usage example is shown next. ngDoCheck This is called during every change detection run. It is a good place for custom logic, which allows us to do a fine-grained check of which property on our object changed. ngAfterContentInit This is called once, after Angular puts external content into the component's view. A placeholder for any external content is marked with the ngContent directive (the ng-content tag). A usage example of the ngContent directive is demonstrated afterwards. ngAfterContentChecked This is called after Angular checks the content put into the component's view. ngAfterViewInit This is called once, after Angular initializes the component's and child's views. ngAfterViewChecked This is called after Angular checks the component's views and child views. ngOnDestroy This is called just before Angular destroys the component's instance. This happens when you remove the component with built-in structural directives such as ngIf, ngFor, ngSwitch, or when you navigate to another view. This is a good place for cleanup operations such as unsubscribing observables, detaching event handlers, canceling interval timers, and so on.

 

Let's see an example of how to use ngOnInit and ngOnChanges:

import {Component, OnInit, OnChanges, SimpleChange} from '@angular/core';

@Component({
selector: 'greeting-component',
template: `<h1>Hello {{text}}</h1>`
})
export class GreetingComponent implements OnInit, OnChanges {
@Input text: string;

constructor() { }

ngOnInit() {
text = "Angular";
}

ngOnChanges(changes: {[propertyName: string]: SimpleChange}) {
console.log(changes.text);
// changes = {'text': {currentValue: 'World', previousValue: {}}}
// changes = {'text': {currentValue: 'Angular',
previousValue: 'World'}}
}
}

Usage in HTML:

<greeting-component [text]="World"></greeting-component>

Let's now see how to use the ngContent directive:

export @Component({
selector: 'greeting-component',
template: `<div><ng-content></ng-content> {{text}}</div>`
})
class GreetingComponent {
@Input text: string;
}

Usage in HTML:

<greeting-component [text]="World"><b>Hello</b></greeting-component>

After the component's initialization, the following hook methods get always executed on every change detection run: ngDoCheck -> ngAfterContentChecked -> ngAfterViewChecked -> ngOnChanges.

主站蜘蛛池模板: 峡江县| 廊坊市| 舞阳县| 龙州县| 嘉祥县| 大埔区| 宣城市| 灵武市| 南漳县| 昌黎县| 石首市| 芷江| 蛟河市| 佛山市| 凤翔县| 阿拉善左旗| 东乌| 建平县| 孟州市| 哈密市| 兴和县| 安达市| 阿拉善盟| 新宁县| 许昌县| 无锡市| 泰和县| 福海县| 子长县| 巴塘县| 鄢陵县| 肇东市| 乳源| 武胜县| 金平| 塔河县| 航空| 攀枝花市| 抚州市| 涿州市| 江口县|