Angular Components Cheatsheet

Complete Angular reference: decorators, components, services, lifecycle hooks, directives.

FeatureDescriptionSyntaxExampleCategory
@ComponentDefine a component@Component({ selector, template, styles })@Component({ selector: "app-root", templateUrl: "app.html" })Decorators
@Input()Accept input from parent@Input() propertyName: type@Input() name: stringProperties
@Output()Emit events to parent@Output() eventName = new EventEmitter()@Output() onClick = new EventEmitter<string>()Properties
@ViewChild()Access child component@ViewChild(ChildComponent) child: ChildComponent@ViewChild("template") tmpl: TemplateRefProperties
@ViewChildren()Access multiple children@ViewChildren(ChildComponent) children: QueryList@ViewChildren(ItemComponent) items: QueryList<ItemComponent>Properties
@ContentChild()Access projected content@ContentChild(ProjectedComponent) content@ContentChild(PanelComponent) panelProperties
ngOnInit()Component initializationngOnInit(): void { }ngOnInit() { this.loadData() }Lifecycle
ngOnChanges()Detect input changesngOnChanges(changes: SimpleChanges)ngOnChanges(changes) { console.log(changes.name.currentValue) }Lifecycle
ngOnDestroy()Cleanup before destroyngOnDestroy(): void { }ngOnDestroy() { this.subscription.unsubscribe() }Lifecycle
@Injectable()Mark class as service@Injectable({ providedIn: "root" })@Injectable() class UserService { }Services
Dependency InjectionInject dependenciesconstructor(private service: ServiceName) { }constructor(private http: HttpClient) { }Services
Template SyntaxBind data to template{{ expression }} / [property]="expression"<div [class]="className">{{title}}</div>Directives
(event)Event binding(eventName)="handler()"<button (click)="increment()">Click</button>Directives
*ngIfConditional rendering*ngIf="condition"<div *ngIf="isVisible">Visible</div>Directives
*ngForLoop through items*ngFor="let item of items"<li *ngFor="let user of users">{{user.name}}</li>Directives