Performance: Template reference for direct access
MEDIUM IMPACT
This affects interaction speed and rendering efficiency by reducing the need for extra DOM queries or component lookups.
<input #inputRef />
<button (click)="inputRef.focus()">Focus Input</button>import { ElementRef, ViewChild, Component } from '@angular/core'; @Component({ selector: 'app-example', template: `<input #inputRef />` }) export class ExampleComponent { @ViewChild('inputRef') inputElement!: ElementRef; focusInput() { this.inputElement.nativeElement.focus(); } }
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| @ViewChild querying | Extra DOM query during lifecycle | 0-1 reflows depending on usage | Minimal paint cost | [!] OK but slower |
| Template reference direct access | No extra DOM query, direct reference | 0 reflows | Minimal paint cost | [OK] Good |