Angular turns your component template into generated render/update instructions (AOT or JIT). At runtime, change detection re-runs the update phase so bindings (text, properties, attributes, events) stay in sync with the component instance (state). Covers: angular, templates, compilation, data binding, basics.
How does Angular compile templates and bind them to component state?
Use guided tracks for structured prep, then practice company-specific question sets when you want targeted interview coverage.
Overview
Angular does two big jobs:
1) Compile the template into efficient instructions (Ivy) + type-check it.
2) Run change detection to execute the template’s update instructions, keeping the DOM synced with the component instance.
Step | What happens | Why it matters |
|---|---|---|
Template parse | HTML + Angular syntax become an internal AST (elements, bindings, directives, pipes). | Angular understands what must be created vs updated. |
Template scope resolution | Figure out which directives/pipes/components are legal (standalone | Missing imports become compile errors. |
Template type-checking | Angular generates TS type-check blocks so your bindings are checked by TypeScript. | Catches |
Code generation (Ivy) | Compiler emits a template function with create and update phases (instructions). | Runtime is fast: no string templates, no regex, minimal DOM work. |
Bundling (AOT) or runtime compile (JIT) | AOT ships generated JS; JIT compiles in the browser (dev-style). | AOT = smaller/faster startup + earlier errors. |
import { Component, signal } from '@angular/core';
@Component({
selector: 'app-counter',
standalone: true,
template: `
<h3>Hello, {{ name }}</h3>
<button [disabled]="count() >= 3" (click)="inc()">
Count: {{ count() }}
</button>
`
})
export class CounterComponent {
name = 'Ada';
count = signal(0);
inc() {
this.count.update(v => v + 1);
}
}
What the compiler roughly generates (simplified)
Angular creates a template(rf, ctx) function. rf is a flag: create vs update. ctx is the component instance.
function CounterComponent_Template(rf: number, ctx: CounterComponent) {
if (rf & 1) {
// CREATE phase: create DOM nodes + wire listeners once
// ɵɵelementStart(0, 'h3');
// ɵɵtext(1);
// ɵɵelementEnd();
// ɵɵelementStart(2, 'button');
// ɵɵlistener('click', () => ctx.inc());
// ɵɵtext(3);
// ɵɵelementEnd();
}
if (rf & 2) {
// UPDATE phase: re-evaluate bindings and patch DOM as needed
// ɵɵtextInterpolate1('Hello, ', ctx.name, '');
// ɵɵproperty('disabled', ctx.count() >= 3);
// ɵɵtextInterpolate1('Count: ', ctx.count(), '');
}
}
Binding in template | Compiled into | When it runs |
|---|---|---|
Interpolation | Text update instruction (set text node value). | Every change detection update pass. |
Property binding | Property update instruction (set DOM property). | Every update pass; only patches if value changed. |
Attribute/class/style bindings | Dedicated attribute/class/style instructions. | Every update pass; optimized diffs. |
Event binding | Listener instruction storing a callback referencing | Listener is created once; runs on events. |
Directive/component inputs | Input set instructions calling directive/component input setters. | Every update pass when values change. |
How the template “binds to component state”
Bindings read from the component instance (ctx). When state changes, Angular runs change detection and re-executes the template’s update phase, re-reading ctx and patching the DOM.
What triggers an update pass | Typical examples |
|---|---|
User events | click/input/submit handlers run, state changes, Angular checks affected views. |
Async completion | timers, HTTP responses, observable emissions, promise resolution (via zone or explicit scheduling). |
Manual signals / APIs | signals update, |
Input changes from parent | Parent updates child input binding; child updates in the next pass. |
AOT vs JIT | AOT (build-time) | JIT (runtime) |
|---|---|---|
When compiled | During build; ships compiled instructions. | In the browser; compiles templates at runtime. |
Startup performance | Better (no runtime compilation cost). | Worse (extra work at runtime). |
Error timing | More errors caught earlier (type-check + template checks). | Some errors appear later (at runtime). |
Typical usage | Production builds. | Dev workflows / special cases. |
Practical implications (interview-relevant) | What to say |
|---|---|
Templates are not “magic strings” | They compile to create/update instructions; update phase re-runs on CD. |
Keep templates cheap | Avoid heavy computations in bindings; move to component and cache/derive. |
OnPush changes behavior | OnPush reduces checks; updates happen on input reference change, events, async, or explicit marking. |
Structural directives change the tree |
|
Practical notes
Watch for edge case behavior, common pitfalls, and trade-offs between clarity and performance. Mention accessibility and testing considerations when the concept affects UI output or event timing.
Angular compiles templates into Ivy render instructions with a create and update phase. The update phase re-reads values from the component instance and patches the DOM during change detection, which is how bindings stay synced with component state.