0 day streak
javascript / beginner
Snippet

Lifecycle Hooks: ngOnInit

The ngOnInit hook is called by Angular after the component's data-bound properties are first initialized. It is the best place for initialization logic like fetching data.

snippet.js
javascript
1
2
3
4
5
export class MyComponent implements OnInit {
ngOnInit(): void {
console.log('Component initialized!');
}
}
angular
Breakdown
1
implements OnInit
This TypeScript interface ensures your class includes the ngOnInit method.
2
ngOnInit(): void
The logic inside this method runs exactly once after the component is created.