- Angular UI Development with PrimeNG
- Sudheer Jonna Oleg Varaksin
- 484字
- 2021-07-15 17:32:57
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:
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.
- Android Wearable Programming
- VMware View Security Essentials
- Python程序設計教程(第2版)
- 嵌入式軟件系統測試:基于形式化方法的自動化測試解決方案
- 微信小程序入門指南
- Teaching with Google Classroom
- C/C++程序員面試指南
- Mastering ArcGIS Enterprise Administration
- Android嵌入式系統程序開發:基于Cortex-A8(第2版)
- Python大學實用教程
- Bootstrap for Rails
- QPanda量子計算編程
- 零基礎看圖學ScratchJr:少兒趣味編程(全彩大字版)
- 軟件設計模式(Java版)
- Java Web開發任務教程