Sub topic 3
Exploring the component tree
This chapter assumes you can read standalone Angular components and templates, and that you have used the browser's Elements panel. You also need Angular DevTools installed, with an app served in development mode (for example with ng serve). The previous chapter covered installation. Here you learn to read the Components tab, the starting point for most debugging in DevTools.
What the tree shows
The Components tab shows a tree of the Angular components and directives that exist in the running page. It is not the DOM tree. The Elements panel lists every <div> and <span>. The component tree lists only the nodes Angular created from your component classes, plus directives attached to elements.
Each row is one instance. If a list renders 50 app-item components, you see 50 rows, each with its own state. The nesting follows the template structure: a component is a child of the component whose template declares it.
This difference matters when debugging. A question like "which component owns this button?" is easy in the component tree. A question like "which CSS rule styles this button?" belongs in the Elements panel.
A sample app to explore
The following app has a root component, a header, and a list of items. Use it, or any app of your own, to follow along.
import { Component, input, output } from '@angular/core';
import { bootstrapApplication } from '@angular/platform-browser';
@Component({
selector: 'app-item',
standalone: true,
template: `
<li>
{{ label() }}
<button (click)="selected.emit(label())">Select</button>
</li>
`,
})
export class ItemComponent {
label = input.required<string>();
selected = output<string>();
}
@Component({
selector: 'app-header',
standalone: true,
template: `<h1>{{ title() }}</h1>`,
})
export class HeaderComponent {
title = input('Inventory');
}
@Component({
selector: 'app-root',
standalone: true,
imports: [HeaderComponent, ItemComponent],
template: `
<app-header title="Warehouse" />
<ul>
@for (name of names; track name) {
<app-item [label]="name" (selected)="current = $event" />
}
</ul>
<p>Current: {{ current }}</p>
`,
})
export class AppComponent {
names = ['Bolts', 'Nuts', 'Washers'];
current = '';
}
bootstrapApplication(AppComponent);Open the app, open the browser developer tools, and switch to the Angular panel. In the Components tab you should see app-root at the top. Under it are one app-header and three app-item rows. The @for block does not appear as a node, because control flow is not a component.
Reading the tree
Rows show the component's selector name, and you expand and collapse them with the arrow beside each row. When a node has children, DevTools collapses deep branches by default in large apps, so expand a branch before concluding a component is missing. The keyboard works as well: arrow keys move the selection, and left and right arrows collapse and expand a node.
Several things can surprise you in what the tree includes.
- Directives. An attribute directive such as
[ngModel]or your own[appHighlight]appears as a node attached to its host element. The host component, if there is one, is listed with it. - Components in other roots. A page can bootstrap more than one Angular application. DevTools lets you pick which root to inspect, and a tree for one root does not show the other.
- Components that are not rendered. A component inside a false
@ifbranch, or a route that is not active, has no instance. It is absent from the tree until it is created. - Components after destruction. When a view is destroyed, its rows disappear on the next refresh of the tree. DevTools updates as the app runs, so a row vanishing is a useful sign that cleanup worked.
Finding a component quickly
Real apps have hundreds of nodes, so scrolling is not practical. DevTools gives you three ways to locate a component.
- Search. Type in the search box above the tree to filter by component or directive name. Typing
itemfinds eachapp-itemrow. Use this when you know the name. - Inspect from the page. Click the "inspect element" icon at the top of the Components tab, then click an element on the page. DevTools selects the matching component in the tree. Use this when you can see the broken UI but do not know which component renders it.
- Select in the Elements panel. If you have an element selected in the browser's Elements panel, the console variable
$0holds it. You can read its component from the console, which is covered in a later chapter.
The second method is the one you will use most. It maps visual symptoms to code with no knowledge of the app's structure.
When you hover a row in the tree, DevTools highlights the corresponding element on the page. This is a quick check that you have found the right instance, especially among repeated rows. In the sample app, hovering over each app-item highlights one list entry at a time.
Selecting a node
Clicking a row selects it and opens its details on the right: properties, inputs, outputs, and state. The next chapter covers those panels in depth. For now, notice how selection links the tree to everything else in the tool.
Selection also gives you a way to compare instances. Select the first app-item, note its label, then select the second. You see the same class with different values. That is often how you confirm that a bug is data-dependent and not logic-dependent.
Reading structure to find problems
The tree is not only a map. Its shape tells you things.
| What you see | What it can mean |
|---|---|
| Many identical rows where you expect few | A list is rendering more items than intended, or a `track` expression is missing and items are recreated |
| A component nested deeper than expected | A template declares it inside another component's template, which affects input flow and injector lookup |
| A row missing for a visible element | The element comes from plain HTML or a non-Angular library, not an Angular component |
| Rows repeatedly appearing and disappearing | Something toggles `@if` or the router repeatedly, destroying and recreating instances |
For the last case, watch the tree while you interact with the page. If the same rows flash in and out on every keystroke, a parent is probably creating new data objects or toggling a condition on each change. You can confirm this later with the profiler.
Limits of the tree
Know what the tree cannot tell you, so you do not waste time on it.
- Development mode only. Production builds strip the debugging hooks DevTools needs. If the panel reports that it cannot detect Angular, or that the app is in production mode, serve a development build.
- Rendering is not shown. The tree has no information about HTML output. A component can exist in the tree and still render nothing visible, for example because of CSS.
- Not every DOM node is a component. Content from
innerHTML, third-party widgets, and web components created without Angular does not show up. - It is a snapshot with live updates. If the tree looks stale after a major change, use the refresh control or reselect the root to rebuild it.
A short walkthrough
Use the sample app to practice the full loop of locating and confirming a component.
- Start the app with
ng serveand open it in Chrome or another supported browser. - Open the developer tools and choose the Angular panel, then the Components tab.
- Confirm that
app-rootappears with oneapp-headerand threeapp-itemchildren. - Type
iteminto the search box and check that three rows remain. - Clear the search, click the "inspect element" icon, and click the "Nuts" row on the page. The tree should select the second
app-item. - Hover over each
app-itemrow and watch the matching list entry highlight on the page.
If step 3 shows nothing, you are probably serving a production build, or the page has not finished bootstrapping. Reload the page with the Angular panel open.
Exercise
A teammate says the "Total" widget on a dashboard is "not an Angular component, because I can't find it in the Components tab." You can see the widget on the page. Name two possible explanations and one way to check each.
Show answer
First, the widget may be created by non-Angular code, such as a third-party library or plain HTML inserted with innerHTML. Check by selecting its element in the Elements panel and looking at whether its ancestors belong to an Angular component, or by using the "inspect element" tool and seeing whether DevTools selects any component. Second, the widget may be an Angular component under another root, or the app may be a production build where DevTools cannot read the tree. Check by looking at the root selector in DevTools for other roots, and by confirming the app is served in development mode with ng serve. A third possibility is that the widget is rendered by a parent component's template with plain elements, so the widget has no component of its own. In that case, selecting the closest component in the tree gives you its parent.