Sub topic 13
Putting it together: a debugging workflow
This chapter combines the earlier panels into one repeatable routine. It assumes you can read Angular components and templates, you know TypeScript, and you have used the Elements and Console panels. It also assumes you have Angular DevTools installed and your app running in development mode.
Start with a symptom
Debugging goes faster when you name the symptom first, because the symptom tells you which panel to open.
| Symptom | First tool | What you look for |
|---|---|---|
| Wrong value on screen | Component explorer | The property or input the template reads |
| Child shows stale data | Component explorer | The input value the parent actually passes |
| Service behaves oddly | Injector tree | Which injector provides the instance |
| UI feels slow or laggy | Profiler | Long change detection cycles |
| Need a quick experiment | Console `ng` global | Component instance and manual re-render |
The workflow
Step 1: Locate the component
Select the element in the Elements panel, then switch to the Angular tab. The component explorer highlights the owning component. You can also search the tree by name. Check its inputs and properties against what the template shows.
Step 2: Reproduce the state
Edit the suspect property in the explorer. If the screen changes the way you expect, the template is correct and the bug is in the code that sets that value. If it does not change, the component may use OnPush, or the template may read a different property. Use the view-source button to jump to the component's file.
Step 3: Measure before you change anything
For slowness, record a profiler session while you repeat the slow interaction. Look for tall bars, then select one to see which components and directives took the time. Note the numbers. Without a baseline you cannot tell whether a fix helped.
Step 4: Apply one fix and record again
Change one thing at a time. For example, move a costly template method into a computed signal and set OnPush:
import { ChangeDetectionStrategy, Component, computed, signal } from '@angular/core';
@Component({
selector: 'app-report',
standalone: true,
changeDetection: ChangeDetectionStrategy.OnPush,
template: `
@for (row of totals(); track row.id) {
<p>{{ row.name }}: {{ row.total }}</p>
}
`,
})
export class ReportComponent {
rows = signal(Array.from({ length: 500 }, (_, i) => ({ id: i, name: `Row ${i}`, values: [i, i + 1, i + 2] })));
totals = computed(() =>
this.rows().map((r) => ({
id: r.id,
name: r.name,
total: r.values.reduce((a, b) => a + b, 0),
}))
);
}Record the same interaction again and compare bar heights with your baseline.
Step 5: Use the console for fast experiments
When you need something the panels do not offer, the ng global works on the element you selected ($0):
const cmp = ng.getComponent($0);
console.log(cmp);
cmp.title = 'Changed from the console';
ng.applyChanges(cmp);ng.applyChanges forces a re-render, so it is useful for OnPush components that do not update after a direct property assignment.
Common mistakes
- Fixing several things between recordings. You then cannot tell which change helped.
- Comparing recordings of different interactions. Repeat the same steps each time.
- Trusting an edit in the explorer as a fix. It only changes the running instance, and a reload discards it.
- Mutating an object in an
OnPushcomponent and expecting a refresh.OnPushreacts to new references, signal changes, and events, so replace the object or update a signal.
Exercise
A list component shows stale data after a parent updates an object in place. The component uses OnPush. Using the workflow in this chapter, how do you confirm the cause and fix it?
Show answer
Select the component in the explorer and compare its input with what the parent holds. The input points to the same object, so OnPush sees no new reference and skips the check. To confirm, call ng.applyChanges on the component from the console and see the view update. Fix it in the parent by passing a new object (for example with spread syntax) or by using a signal, then verify in the app.
Exercise
Why should you record a profile before applying an optimization, and what should stay the same between the two recordings?
Show answer
The first recording is the baseline that shows whether the fix helped. The interaction you perform, the app state, and the build mode should stay the same, and you should change only one thing between recordings.