Sub topic 13
Putting it together: a debugging workflow
This chapter combines the Angular DevTools panels and the ng console utilities into one repeatable routine. It assumes you have read the earlier chapters on the component explorer, state inspection, the profiler, and OnPush. It also assumes you can run an Angular app in development mode with the DevTools extension installed. Each step here is covered in detail elsewhere, so this chapter is about order and decisions.
Start with a symptom, not a tool
Opening every panel and looking around wastes time. Name the symptom first, then pick the panel that can confirm or rule out a cause.
| Symptom | First suspect | Where to look first |
|---|---|---|
| A value on screen is wrong | Wrong input, or stale state in the component | Component explorer, properties and inputs |
| The screen does not update after data changes | The component is not marked for check (OnPush with a mutated object) | Explorer, then the console `ng.applyChanges` test |
| A service returns unexpected data | Wrong provider or injector level | Injector tree and the component's dependencies |
| A page shows the wrong route or component | Route configuration or guard | Router tree |
| Typing or scrolling feels slow | Long change detection cycles | Profiler |
Use the table as a starting point, not a rule. A stale view can come from an injector problem, and a slow view can come from a wrong input that triggers extra work.
The workflow at a glance
The loop has two branches. If the data is wrong, you work with the explorer. If the data is right but the view is slow or stale, you work with change detection and the profiler. Checking state first keeps you from profiling a bug that is really a logic error.
Step 1: Reproduce and narrow the scope
Reproduce the problem with the smallest sequence of user actions you can. A short, repeatable sequence matters for two reasons:
- You can compare behavior before and after a change.
- In the profiler, a short recording is easier to read than a long one.
Write the sequence down. For example: "Open /reports, type one character in the filter box." Use exactly that sequence every time you test.
Step 2: Locate the component
Select the element in the browser's Elements panel, then switch to the Angular tab. Selecting the element in the Elements panel makes $0 point at it, and the same node can be found in the component explorer. You can also use the explorer's search box or its element picker, which lets you click the page and jump to the owning component.
Note three things as you select:
- The component's class name, so you know you are looking at the right one.
- Whether it is inside another component you did not expect, which can point to a template mistake.
- Its host element, since a directive on the same element may also be involved.
Step 3: Inspect state, then edit it
In the properties view, check inputs against what the parent template should be passing. If an input is undefined or has the wrong shape, the problem is upstream, so select the parent and check what it binds.
When the state looks plausible, test your idea by editing it. Change a property in the DevTools property view, or use the console:
// $0 is the element selected in the Elements panel.
const cmp = ng.getComponent($0);
console.log(cmp);
// Try the state you suspect is broken.
cmp.items = [];
ng.applyChanges(cmp);If the view updates after ng.applyChanges(cmp) but not on its own, the component's data changed without Angular noticing. That result points to an OnPush or mutation problem, not a data problem. If the view is wrong even after applyChanges, the template logic is at fault.
Editing in DevTools changes only the running instance. A page reload discards the edit. Once an edit proves your theory, move the fix into the source code.
Step 4: Check what was injected
If the component holds a service and that service behaves wrongly, check which instance it received. Look at the injector tree for the component. A service provided in both the root injector and in a component's providers array gives you two separate instances. A common symptom is that a value set in one part of the app is "missing" in another.
From the console you can check the same thing:
const injector = ng.getInjector($0);
console.log(injector);Compare the instance a component receives with the one another component receives. If they differ, look for an extra providers entry or a lazily loaded route that creates its own injector.
Step 5: Profile when the state is right but the view is slow
Open the Profiler tab and record only the reproduction sequence from step 1. Then read the results:
- Look at the bar chart of change detection cycles. Tall bars are slow cycles.
- Select the tallest bar. The flame graph or ranked view shows which components and directives used the time.
- Check how often cycles happen, not only how long each takes. Many short cycles during a mouse move or scroll can cost as much as one long one.
Write down the slowest cycle's duration. You need it for the comparison later.
A measured example
The component below calls a costly method from its template. With default change detection, every cycle runs the method for all 500 rows.
import { Component } from '@angular/core';
@Component({
selector: 'app-report',
standalone: true,
template: `
@for (row of rows; track row.id) {
<p>{{ row.name }}: {{ expensiveTotal(row) }}</p>
}
`,
})
export class ReportComponent {
rows = Array.from({ length: 500 }, (_, i) => ({ id: i, name: `Row ${i}`, values: [i, i + 1, i + 2] }));
// Called for every row on every change detection pass.
expensiveTotal(row: { values: number[] }): number {
let total = 0;
for (let n = 0; n < 100000; n++) {
total += row.values[n % row.values.length];
}
return total;
}
}In the profiler, ReportComponent dominates every cycle, even when the cycle was triggered by something unrelated, such as a click elsewhere on the page. That pattern, the same component in every bar, is the signal that work in the template is being repeated for no reason.
The fix has two parts. Move the calculation out of the template so it runs only when data changes, and use OnPush so Angular skips the component when nothing it depends on changed.
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] })));
// Recomputed only when `rows` changes.
totals = computed(() =>
this.rows().map((r) => ({
id: r.id,
name: r.name,
total: r.values.reduce((a, b) => a + b, 0),
}))
);
}Record the same sequence again and compare it with the number you wrote down. The cycle that triggers when you click elsewhere should no longer spend time in ReportComponent.
Step 6: Verify, then keep the evidence
A fix is verified when the same reproduction sequence gives a better result. For a state bug, the screen is correct without any console help. For a performance fix, the slowest cycle is shorter and the previously dominant component is gone from unrelated cycles. Keep the before and after numbers in your commit message or pull request. They show reviewers that the change did something.
Common errors in the workflow
| Mistake | What goes wrong | Better approach |
|---|---|---|
| Profiling a production build | DevTools does not detect the app, or the data is missing | Serve in development mode |
| Recording a long session | The bar chart is crowded and the slow cycle is hard to find | Record only the reproduction sequence |
| Changing several things at once | You cannot tell which change helped | Change one thing, then record again |
| Editing state in DevTools and calling it fixed | The edit disappears on reload | Move the confirmed fix into the source |
| Adding OnPush and mutating objects | The view stops updating | Use signals, or replace objects with new copies |
| Comparing recordings from different action sequences | The numbers are not comparable | Repeat the exact same steps |
The OnPush mutation trap
The most common regression after adding OnPush is a view that stops updating. This component mutates an array in place, so the input reference never changes:
import { ChangeDetectionStrategy, Component, input } from '@angular/core';
@Component({
selector: 'app-tags',
standalone: true,
changeDetection: ChangeDetectionStrategy.OnPush,
template: `
@for (tag of tags(); track tag) {
<span>{{ tag }}</span>
}
`,
})
export class TagsComponent {
tags = input.required<string[]>();
}If a parent calls this.tags.push('new'), TagsComponent is not checked, because the array reference is the same. To confirm this in DevTools, select TagsComponent and inspect its tags input. The input will already contain 'new', so the data is right and only the view is stale. Then run ng.applyChanges(ng.getComponent($0)) in the console. If the screen now shows the new tag, you have the diagnosis. In the parent, replace the array instead: this.tags = [...this.tags, 'new'], or keep the list in a signal and call update.
When DevTools is not enough
DevTools shows what Angular does, not what the browser does afterward. If the profiler reports short change detection cycles but the page still feels slow, the cost is likely in layout, painting, or network. Use the browser's own Performance panel for those. If the component tree looks correct and the profiler is quiet, the bug may sit in the HTTP layer or an RxJS pipeline, where a breakpoint or a tap operator is a better tool.
Exercises
Exercise
A component with OnPush does not show a new item after the parent calls items.push(item). The DevTools property view shows the new item inside the component's items input. Running ng.applyChanges(ng.getComponent($0)) makes it appear. What is the diagnosis and the fix?
Show answer
The data is correct, but the input reference did not change, so OnPush never marked the component for checking. The fix is to give the child a new array, for example this.items = [...this.items, item], or to hold the list in a signal and update it with update.
Exercise
You add OnPush to a component, record a new profile, and see that the longest cycle is only slightly shorter. Name two reasons this can happen and one way to find out which applies.
Show answer
First, the expensive work may still run in the template (for example a method call) every time the component is checked, and the component may still be checked often because its inputs change. Second, a different component, such as a child or a sibling, may be the real cost. Open the flame graph or ranked view for the slowest cycle to see which component or directive takes the time, then change one thing at a time and record the same sequence again.
Exercise
Two components both inject CartService, but one shows an empty cart and the other shows items. List the steps you would take with DevTools and the console to find the cause.
Show answer
Select each component in the explorer and look at its injected dependencies or its injector path. Compare the two CartService instances, for example with ng.getInjector($0) and a lookup of the service from each. If they differ, look for a providers entry on a component or route that creates a second instance, and remove it or move the provider so both share one instance.