Sub topic 1
The component tree and its views
Change detection is the process that keeps the DOM in sync with component state. Before you can reason about when Angular runs it, you need a clear picture of what it walks over. This chapter describes the component tree and the view that each component owns.
Prerequisites
This chapter assumes you already know:
- How to write a basic Angular component with a template.
- TypeScript classes and decorators.
- Template binding syntax: interpolation, property bindings and event bindings.
- Component inputs and outputs.
You do not need to know anything about Zone.js or signals yet. Later chapters cover them.
Components form a tree
Every Angular application starts from one root component. That component's template uses other components, and those components use others in turn. The result is a tree.
import { Component } from '@angular/core';
@Component({
selector: 'app-item',
template: `<li>{{ label }}</li>`,
})
export class Item {
label = 'Item';
}
@Component({
selector: 'app-list',
imports: [Item],
template: `<ul><app-item /><app-item /></ul>`,
})
export class List {}
@Component({
selector: 'app-root',
imports: [List],
template: `<h1>Shop</h1><app-list />`,
})
export class App {}Here App is the root. It contains one List, and the List contains two Item components. The tree is defined by what each template instantiates, not by file layout or class inheritance.
Each component owns a view
At runtime, Angular creates a view for every component instance. A view is the internal record of one component's template. It holds:
- The DOM nodes the template created.
- The current values of each binding expression, from the last time Angular checked.
- The component instance the template reads from.
Because the List above contains two Item components, there are two separate Item views. They share a template but not state. Each view remembers its own last-seen binding values.
Views are arranged in the same shape as the component tree. A parent view contains the views of its child components, so Angular can start at the root view and reach every other view by walking downward.
What a view is used for
During change detection, Angular visits a view and re-evaluates each binding expression in its template. It compares the new value with the one stored in the view. If the value differs, Angular updates the matching DOM node. If it is the same, Angular leaves the DOM alone.
So the view serves two purposes:
- It is the unit that Angular checks. Change detection operates on views, one at a time.
- It stores the previous binding values, which makes the comparison possible.
The next chapter covers exactly which values are compared and how. For now, remember that the tree decides the order of visits and the view holds the data needed to decide what changed.
Direction of the walk
Angular checks views from the root downward, parent before child. A parent's bindings are updated first, which means new input values reach a child before the child's own template is checked. This ordering is what lets data flow in one direction, from parent to child.
Why the shape matters
The tree explains most of the behavior you will meet later in this tutorial:
- Checking the whole application means visiting every view, so more components means more work per cycle.
- Skipping a view can mean skipping everything beneath it, which is the basis of the OnPush strategy.
- A change in a child component's state can still require work in its ancestors, because the walk starts at the root.
Exercise
In the example, how many views does Angular create for Item, and why can they hold different values even though they share one template?
Show answer
Two, because List instantiates Item twice. Each instance gets its own view, and each view stores its own component instance and its own last-seen binding values. The template is shared, but the state is not.