![]() |
VOOZH | about |
In Angular, Components are the fundamental building blocks of an application. Understanding the lifecycle of these components is crucial for effective Angular Development. Angular provides several lifecycle hooks that allow developers to tap into key moments in a Component’s lifecycle and execute custom logic during those times.
The component lifecycle in Angular consists of several stages:
Angular provides a set of lifecycle hooks that allow developers to execute code at specific stages of a component’s lifecycle.
It is called before ngOnInit (if the component has bound inputs) and whenever one or more data-bound input properties change. It is used to respond to changes in input properties.
ngOnChanges(changes: SimpleChanges) {
console.log('Changes detected:', changes);
}
It is Called once, after the first ngOnChanges. It is used to initialize the component after Angular first displays the data-bound properties.
ngOnInit() {
console.log('Component initialized');
}
It is called during every change detection run, immediately after ngOnChanges and ngOnInit. It is used to detect and act upon changes that Angular can't or won't detect on its own.
ngDoCheck() {
console.log('Custom change detection');
}
It is called once after the first ngDoCheck. It is used to perform any additional initialization required for the content.
ngAfterContentInit() {
console.log('Content initialized');
}
It is called after ngAfterContentInit and every subsequent ngDoCheck. It is used to act upon any changes after the content has been checked.
ngAfterContentChecked() {
console.log('Content checked');
}
It is called once after the first ngAfterContentChecked. It is used to perform additional initialization required for the view.
ngAfterViewInit() {
console.log('View initialized');
}
It is called after ngAfterViewInit and every subsequent ngAfterContentChecked. It is used to act upon any changes after the view has been checked.
ngAfterViewChecked() {
console.log('View checked');
}
It is called immediately before Angular destroys the component. It is used to clean up any resources, such as subscriptions and event handlers, to avoid memory leaks.
ngOnDestroy() {
console.log('Component destroyed');
}
Ensure you have Node.js installed. Then, install Angular CLI globally:
npm install -g @angular/cliCreate a new Angular project:
ng new angular-gfg
cd angular-gfg
Generate a new standalone component called user-profile:
ng generate component user-profile --standalone"dependencies": {
"@angular/animations": "^17.3.0",
"@angular/common": "^17.3.0",
"@angular/compiler": "^17.3.0",
"@angular/core": "^17.3.0",
"@angular/forms": "^17.3.0",
"@angular/platform-browser": "^17.3.0",
"@angular/platform-browser-dynamic": "^17.3.0",
"@angular/router": "^17.3.0",
"rxjs": "~7.8.0",
"tslib": "^2.3.0",
"zone.js": "~0.14.3"
}
Run the application to see the lifecycle hooks in action:
ng serveOpen your browser and navigate to http://localhost:4200. You should see the component displayed along with the console logs showing the lifecycle hook calls.