Sub topic 3
Exploring the component tree
This chapter assumes you have Angular DevTools installed and your app running in development mode, as covered in the previous chapter. It also assumes you know how Angular components nest inside templates. Here you will learn to read the Components tab and move around it quickly.
What the tree shows
The Components tab of Angular DevTools shows the structure Angular built at runtime. It is not the DOM. The Elements panel lists every <div> and <span>. The component tree lists only the components and directives Angular created, nested the way their templates nest.
That difference matters when you debug. A component's host element may sit under several plain wrapper elements, and the tree hides them. You see which component owns which child, and that is usually the relationship you need.
Consider this small app:
import { Component } from '@angular/core';
@Component({
selector: 'app-badge',
standalone: true,
template: `<span>{{ label }}</span>`,
})
export class BadgeComponent {
label = 'New';
}
@Component({
selector: 'app-card',
standalone: true,
imports: [BadgeComponent],
template: `<div class="card"><app-badge /></div>`,
})
export class CardComponent {}
@Component({
selector: 'app-root',
standalone: true,
imports: [CardComponent],
template: `
<main>
<app-card />
<app-card />
</main>
`,
})
export class AppComponent {}DevTools displays it like this:
The <main> and <div class="card"> elements do not appear. Each app-card is a separate instance with its own app-badge.
Moving around the tree
Use these controls to find a component quickly.
- Expand and collapse. Click the arrow beside a node to show or hide its children. In a large app, collapse everything except the branch you care about.
- Select a node. Clicking a component opens its properties in the panel on the right. The next chapter covers that panel in detail.
- Hover to highlight. Hovering over a node outlines the matching element in the page, which confirms you picked the right instance.
- Search. The search box filters the tree by component name. Type
cardto jump to eachapp-cardinstance without scrolling. - Pick from the page. The inspect (crosshair) button lets you click an element in the running page. DevTools then selects the component that owns it. Use this when you can see the problem on screen but do not know the component's name.
Components versus directives
Directives also appear in the tree. A directive such as NgClass, or one you wrote, shows as part of the element it is attached to. Select the node to see the directive's own properties listed with the component's. If you expect a directive and cannot find it, check that it is imported in the component's imports array. A missing import is a common cause.
Common errors
- The tree is empty or shows a warning. The app is probably not in development mode, or the page has not finished bootstrapping. Reload with DevTools open.
- You cannot find a component you expect. It may be inside an
@ifblock that is currently false, or it may be a route that is not active. Angular has not created it, so it cannot be listed. Trigger the condition in the page, and the node appears. - Many identical nodes. Components inside
@forappear once per item. Select each one and compare its inputs to find the item you want. - The panel shows stale content. After navigating, select a node again. The details panel reflects the node you last clicked.
Exercise
In the example app, you open DevTools and search for badge. How many nodes do you expect, and why does the <div class="card"> not appear in the tree?
Show answer
Two nodes, because each of the two app-card instances creates its own app-badge. The <div class="card"> is a plain HTML element and not a component or directive, so Angular DevTools does not list it. Only the Elements panel shows it.