ViewChild is undefined Angular Origin: On your device Last updated 15 Oct 2026

Angular: TypeError: Cannot read properties of undefined (reading 'nativeElement') — @ViewChild used in ngOnInit

A @ViewChild / viewChild() reference is undefined because the view is not created yet, or the element sits inside @if / *ngIf.

Exact message you may see TypeError: Cannot read properties of undefined (reading 'nativeElement')Cannot read property 'nativeElement' of undefinedViewChild undefinedViewChild is undefined in ngOnInitCannot read properties of undefined (reading 'focus')
Seen on: Angular TypeScript

Meaning

View queries are resolved after the view initialises. In ngOnInit they are undefined (unless { static: true } and the element is not inside a conditional). Use ngAfterViewInit, or afterNextRender, and remember elements inside @if exist only when the condition is true.

Common causes

  • Using the query in ngOnInit or the constructor
  • Element inside *ngIf / @if / @for so the query is empty until rendered
  • Wrong template reference name (#canvas vs 'canvasRef')
  • { static: true } on an element inside a conditional
  • Component used before its view is created

⚡ Quick fix

  1. Use ngAfterViewInit() or afterNextRender()
  2. Use { static: true } only for elements that are always in the template
  3. With signals use viewChild('ref') and read it inside effect/after render

Detailed fix by platform

Angular

  1. @ViewChild('canvas') canvas!: ElementRef<HTMLCanvasElement>; ngAfterViewInit() { const ctx = this.canvas.nativeElement.getContext('2d'); }

Code examples

Safe access

typescript
@ViewChild('canvas') canvas?: ElementRef<HTMLCanvasElement>;

ngAfterViewInit() {
  const el = this.canvas?.nativeElement;
  if (!el) return;                       // not rendered (e.g. inside an @if)
  el.getContext('2d')?.fillRect(0, 0, 10, 10);
}

How to diagnose

  1. Hook — ngOnInit or ngAfterViewInit?
  2. Condition — Element inside @if/ngIf?
  3. Reference — Does #name match the query?

🔧 Still not fixed?

Many errors look alike. If the steps above didn’t solve it, one of these is probably what you’re facing:

Where this error sits

Origin: On your device. Errors that are often confused with or caused by this one (click to open):

addEventListener on null — TypeError: Cannot read properties of null (reading 'addEventListener') (JavaScript)addEventListener on n…Cannot find control with name — Angular: Error: Cannot find control with name: 'email' (reactive forms) (Angular)Cannot find control w…exceeded maximum budget — Angular build: Error: bundle initial exceeded maximum budget. Budget 1.00 MB was not met by 120.00 kB with a total of 1.12 MB (Angular)exceeded maximum budg…NG0100 — Angular: NG0100: ExpressionChangedAfterItHasBeenCheckedError: Expression has changed after it was checked (Angular)NG0100NG0950 — Angular: NG0950: Input is required but no value is available yet (input.required / viewChild.required) (Angular)NG0950reading length — TypeError: Cannot read properties of undefined (reading 'length') (JavaScript)reading lengthSchema validation failed with the following errors — Angular CLI: Schema validation failed with the following errors: Data path "" must NOT have additional properties(xyz) (Angular)Schema validation fai…ViewChild…

Explore the map of this error Full error map →

🧠 Still stuck? Analyze your error

Paste the full message, response headers or stack trace — we'll detect the platform and point to the most likely cause.

💬 What worked for others

No tips yet — did something else fix this for you?

Share what worked for you

Tips are checked by an editor before they appear. Links are removed.