ngOnInit and Initialization
Run setup logic when a component starts.
ngOnInit and Initialization is a free Angular Academy lesson on CoddyKit. This is lesson 1 of 4. Any 3 lessons of this learning path are free to read in full — after that, CoddyKit PRO unlocks every lesson, plus hands-on practice with a built-in code editor and a 24/7 AI tutor. It is part of the Angular Academy learning path, and your progress syncs across the web and the CoddyKit app. The Angular Academy course includes 4 lessons in total.
What Are Lifecycle Hooks?
Angular components have a lifecycle managed by the framework: creation, change detection, rendering, and destruction. Lifecycle hooks let you run code at specific moments.
Introducing OnInit
The OnInit hook runs once, after Angular has set the component inputs and rendered the initial view. It is the standard place for initialization logic.
Implementing OnInit
Implement the OnInit interface and add the ngOnInit method. Angular calls it automatically.
import { Component, OnInit } from "@angular/core";
@Component({ selector: "app-demo", template: "" })
export class DemoComponent implements OnInit {
ngOnInit(): void {
console.log("Component initialized");
}
}Constructor vs ngOnInit
The constructor runs first, before inputs are set, and should only do light dependency injection. Use ngOnInit for real setup once inputs are available.
constructor(private http: HttpClient) {
// inputs NOT ready yet
}
ngOnInit() {
// inputs ARE ready here
}Inputs Are Ready in ngOnInit
By the time ngOnInit runs, all @Input properties have their initial values bound from the parent.
@Input() userId!: string;
ngOnInit() {
console.log(this.userId); // available
}Fetching Data in ngOnInit
A common pattern is to load data from a service in ngOnInit, since you have access to inputs and dependencies.
ngOnInit() {
this.userService.getUser(this.userId)
.subscribe(u => this.user = u);
}ngOnInit Runs Once
Unlike change detection hooks, ngOnInit runs exactly once during the component lifetime, right after the first ngOnChanges.
Order Relative to ngOnChanges
If the component has inputs, ngOnChanges fires first with the initial values, then ngOnInit runs. Without inputs, ngOnInit still runs.
// sequence with inputs:
// 1. constructor
// 2. ngOnChanges (first)
// 3. ngOnInitInitializing Forms
Building reactive forms is a classic ngOnInit task because you may need injected services to set up the form.
form!: FormGroup;
ngOnInit() {
this.form = this.fb.group({
name: [""],
email: [""]
});
}Keep Constructors Light
Avoid heavy work in the constructor. It runs before Angular wires inputs, and side effects there make components harder to test and predict.
Async Initialization
You can call async logic from ngOnInit, but the method itself returns void; subscribe or use async patterns inside it.
ngOnInit() {
this.loadAsync();
}
private async loadAsync() {
this.data = await firstValueFrom(this.api.get());
}Quick Check
Test your understanding of OnInit.
Recap
The OnInit hook with ngOnInit runs once after inputs are bound and is ideal for data fetching and form setup. Keep the constructor light and reserve initialization for ngOnInit.
Learn HTML with an AI tutor — free
Write and run real code in your browser, get instant help from a 24/7 AI tutor, and pick up where you left off on the web or in the app.
- Courses
- 38
- Lessons
- 144
Frequently Asked Questions
Is the “ngOnInit and Initialization” lesson free?
Yes — any 3 lessons of the Angular Academy learning path, including “ngOnInit and Initialization”, are free to read in full here on the web. After that, CoddyKit PRO unlocks every lesson, plus interactive practice with a built-in code editor and a 24/7 AI tutor. The Angular Academy course includes 4 lessons in total.
What will I learn in “ngOnInit and Initialization”?
Run setup logic when a component starts. You practise Angular Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Angular Academy?
No prior experience is required. Angular Academy on CoddyKit is structured for beginners through advanced learners, so you can start here or from the beginning and move at your own pace. This is lesson 1 of 4.
How long does the “ngOnInit and Initialization” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Angular Academy lesson?
Yes. Every Angular Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- ngOnInit and Initialization
- ngOnChanges and Input Changes
- ngOnDestroy and Cleanup
- AfterView and AfterContent Hooks