- 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.
- 新編Visual Basic程序設計上機實驗教程
- Processing互動編程藝術
- 匯編語言程序設計(第3版)
- 數(shù)據(jù)結構(C語言)
- 青少年Python編程入門
- 從零開始學C#
- R Data Science Essentials
- 平面設計經(jīng)典案例教程:CorelDRAW X6
- R語言:邁向大數(shù)據(jù)之路(加強版)
- Java Web開發(fā)教程:基于Struts2+Hibernate+Spring
- Unity 5 Game Optimization
- C語言王者歸來
- Mobile Test Automation with Appium
- 現(xiàn)代JavaScript編程:經(jīng)典范例與實踐技巧
- C語言程序設計實驗指導教程