angular-lifecycle
Installation
SKILL.md
Angular Lifecycle Hooks
Version: Angular 21 (2025) Tags: Lifecycle, Hooks, ngOnInit, ngOnChanges
References: Lifecycle Hooks • API
Best Practices
- Use OnInit for initialization
import { OnInit } from '@angular/core';
@Component({})
export class MyComponent implements OnInit {
ngOnInit() {
// Initialize data, call services
this.loadData();
}
}
- Use OnDestroy for cleanup
import { OnDestroy } from '@angular/core';
@Component({})
export class MyComponent implements OnDestroy {
private destroy$ = new Subject<void>();
ngOnDestroy() {
this.destroy$.next();
this.destroy$.complete();
}
}
- Use OnChanges for input changes
import { OnChanges, SimpleChanges } from '@angular/core';
@Component({})
export class MyComponent implements OnChanges {
@Input() data: any;
ngOnChanges(changes: SimpleChanges) {
if (changes['data']) {
console.log('Data changed:', this.data);
}
}
}
- Use AfterViewInit for DOM manipulation
import { AfterViewInit, ViewChild, ElementRef } from '@angular/core';
@Component({})
export class MyComponent implements AfterViewInit {
@ViewChild('el') el!: ElementRef;
ngAfterViewInit() {
this.el.nativeElement.focus();
}
}
- Use AfterContentInit for projected content
import { AfterContentInit, ContentChild } from '@angular/core';
@Component({})
export class MyComponent implements AfterContentInit {
@ContentChild('header') header!: ElementRef;
ngAfterContentInit() {
// Access projected content
}
}
- Use DoCheck for custom change detection
import { DoCheck } from '@angular/core';
@Component({})
export class MyComponent implements DoCheck {
ngDoCheck() {
// Custom change detection
}
}
- Use OnPush with lifecycle
@Component({
changeDetection: ChangeDetectionStrategy.OnPush
})
export class MyComponent {
@Input() data: any;
ngOnChanges(changes: SimpleChanges) {
// OnPush needs explicit change detection trigger
}
}
- Use constructor vs ngOnInit
// Constructor - for dependency injection only
constructor(private service: MyService) {}
// ngOnInit - for initialization logic
ngOnInit() {
this.data = this.service.getData();
}
Related skills
More from oguzhan18/angular-ecosystem-skills
angular-tailwind
ALWAYS use when working with Angular and Tailwind CSS, Tailwind configuration, utility-first CSS, or styling Angular applications with Tailwind.
139angular-animations
>-
137rxjs
ALWAYS use when working with RxJS Observables, operators, and reactive patterns in Angular applications.
135angular-material
ALWAYS use when working with Angular Material components, CDK, or Material Design in Angular applications.
131angular-security
ALWAYS use when working with Angular Security, XSS prevention, CSRF protection, Content Security Policy, or sanitization in Angular applications.
130angular-bootstrap
ALWAYS use when working with Angular Bootstrap, ng-bootstrap, Bootstrap components in Angular, or Bootstrap 5 integration.
129