π Lifecycle Hooks
Tap into key moments of a component's life β from creation to destruction
About This Feature
Angular calls lifecycle hook methods at specific moments during a component's creation, update, and destruction. Implement the corresponding interface to use each hook.
- ngOnChanges β @Input() values changed
- ngOnInit β component initialized (once)
- ngDoCheck β custom change detection
- ngAfterContentInit β projected content initialized
- ngAfterContentChecked β projected content checked
- ngAfterViewInit β component view rendered (access @ViewChild)
- ngAfterViewChecked β component view checked
- ngOnDestroy β just before destruction (cleanup!)
Code Example
@Component({ ... })
export class MyComponent implements
OnChanges, OnInit, DoCheck,
AfterContentInit, AfterContentChecked,
AfterViewInit, AfterViewChecked, OnDestroy {
@Input() value = '';
// Called before ngOnInit and on every @Input change
ngOnChanges(changes: SimpleChanges) {
console.log('Input changed:', changes);
}
// Called once after first ngOnChanges
ngOnInit() { /* initialize component */ }
// Called after every change detection run
ngDoCheck() { /* custom change detection */ }
// Called after content projection (ng-content)
ngAfterContentInit() { }
ngAfterContentChecked() { }
// Called after component's view is rendered
ngAfterViewInit() { /* access @ViewChild here */ }
ngAfterViewChecked() { }
// Called just before the component is destroyed
ngOnDestroy() { /* cleanup subscriptions */ }
}Live Demo
0 events logged
Hook Event Log (newest first)
Create the child component to see hooks fireβ¦
β Initβ Changesβ DoCheckβ Contentβ Viewβ Destroy