Sub topic 5
Editing Values at Runtime and Viewing Source
What this chapter covers
Earlier chapters showed how to find a component in the tree and read its properties, inputs, and outputs. This chapter shows how to change those values in the running page and how to jump from a component in the tree to its source code.
This chapter assumes you have these prerequisites:
- Angular DevTools is installed, and your app runs in development mode.
- You can select a component in the component explorer and open its properties.
- You know the difference between a component's
@Input()properties, its internal fields, and its signals. - You are comfortable with the browser's Elements, Console, and Sources panels.
Editing is a debugging technique. It changes the live component instance in memory and nothing else. Your source files stay untouched, and a page reload discards every edit.
Why edit state at runtime
Some UI states are slow to reach. An error banner may need a failed request. A "premium user" layout may need a specific account. A long list may need a thousand records. If you can set the property by hand, you skip the setup and see the result in seconds.
Editing also tests a hypothesis. If you think a template misbehaves when errorMessage is empty, set it to empty and look. This separates "the template is wrong" from "the data arriving is wrong."
Editing a value in the Properties panel
Select a component in the component explorer. The Properties panel lists its inputs, outputs, and other fields. To edit a primitive value:
- Double-click the value.
- Type the new value.
- Press Enter to commit it. Pressing Escape, or clicking away without committing, leaves the old value in place.
DevTools sends the new value to the page. Angular's debugging code writes it onto the component instance, and the panel then shows the updated value.
Which values you can edit
Editing works best on primitives. Numbers, strings, and booleans are the reliable cases. Objects, arrays, and functions are shown as expandable trees, and you can often expand them and edit the primitive values inside. You cannot replace a function, and you cannot assign a new object through the panel.
| Value type | Edit in the panel? | Notes |
|---|---|---|
| string, number, boolean | Yes | Type the new value and press Enter. |
| Property inside an object or array | Usually | Expand the tree and edit the leaf value. |
| Whole object or array | No | Use the console and assign it there. |
| Function or method | No | Not editable. |
| Signal | Depends on version | Newer versions display signal values. Setting one is safest from the console. |
| Getter without a setter | No | Edit the field it reads from. |
The exact set of supported types depends on your DevTools version. If the panel refuses an edit, use the console approach later in this chapter.
What happens after you commit
The edit changes the field only. It does not run your code that normally reacts to the change. For example, the edit does not call ngOnChanges, because that hook runs only when a parent binding changes an input. If your component computes derived state in ngOnChanges, that derived state will not update when you edit the input in the panel.
The view then refreshes through change detection. Whether that refresh is visible depends on the component's strategy, as the next section explains.
Editing and OnPush components
Consider a small component that uses OnPush:
import { ChangeDetectionStrategy, Component, signal } from '@angular/core';
@Component({
selector: 'app-status-badge',
standalone: true,
changeDetection: ChangeDetectionStrategy.OnPush,
template: `
@if (errorMessage) {
<p class="error">{{ errorMessage }}</p>
} @else {
<p>Status: {{ status }}</p>
}
<p>Retries: {{ retries() }}</p>
`,
})
export class StatusBadgeComponent {
status = 'idle';
errorMessage = '';
retries = signal(0);
}An OnPush component is checked only when one of its inputs changes by reference, an event fires in its template, an observable bound with async emits, or a signal it reads changes. A direct assignment to a plain field such as errorMessage matches none of these.
So when you edit errorMessage in the panel, the field changes but the screen may stay the same. This is the most common surprise when editing state. The value in the Properties panel is correct and the DOM is stale. Neither one is a bug in your app.
DevTools asks Angular to refresh the component after an edit, and in many versions that is enough. If it is not, force the refresh yourself from the console. The next section shows how.
Editing from the console
The ng global is available in development mode. With a component selected in the Elements panel, $0 refers to its host element. You can fetch the instance, change it, and ask Angular to re-render:
// In the browser console, after selecting an element in the Elements panel ($0):
const cmp = ng.getComponent($0);
console.log(cmp);
// Change a property, then ask Angular to re-render the component:
cmp.title = 'Changed from the console';
ng.applyChanges(cmp);Applied to the status badge, the session looks like this:
const cmp = ng.getComponent($0);
// Plain field: assign, then force a refresh.
cmp.errorMessage = 'Network timeout';
ng.applyChanges(cmp);
// Signal: use its own API. No applyChanges is needed,
// because the template reads the signal and Angular is notified.
cmp.retries.set(3);The console has three advantages over the panel:
- You can assign whole objects and arrays, such as
cmp.rows = []to test an empty state. - You can call methods, such as
cmp.reload(), to trigger real behavior. - You can script a sequence of changes and repeat it after each reload.
ng.getComponent($0) returns null if $0 is not the host element of a component. Select the host element, such as <app-status-badge>, and not an element inside its template. To find the nearest owning component of an inner element, use ng.getOwningComponent($0).
Viewing source
From the component tree you can jump straight to the code that defines a component. Select the component, then use the view source (</>) control in the Properties panel header. In the Angular DevTools versions that provide it, the component tree rows and directive entries have the same kind of control. DevTools then switches to the browser's Sources panel and opens the component's class definition.
This is faster than searching your editor for a selector, especially in a large app where several components have similar names. It is also reliable when a component comes from a library and you do not know which package defines it.
A few details affect what you see:
- Source maps. The Sources panel shows your TypeScript only when source maps are present.
ng servegenerates them by default. If you see compiled JavaScript, check that source maps are enabled in your build configuration. - What opens. The panel opens the class, not the template. For an inline template, the template is part of the decorator in that file. For an external
.htmlfile, open it from the Sources file tree. - Directives. A directive attached to an element can have its own entry in the properties view. Its source control opens the directive class.
- Library code. For components in
node_modules, you land in the library's compiled output. Source maps from the library may or may not be published.
After the jump, you can set a breakpoint in the Sources panel. Combine this with editing: set a breakpoint in a method, edit a value in the panel, then trigger the interaction that calls the method. The debugger stops with your edited state in place.
A workflow that combines both
- Reproduce the problem and select the component with the wrong output.
- Read its properties and note any value that looks wrong.
- Edit the suspect value to a known good one. If the UI becomes correct, the template is fine and the cause is upstream data.
- If the UI does not update, call
ng.applyChanges(cmp)in the console to rule out anOnPushrefresh problem. - Use view source to open the class. Set a breakpoint where the value is assigned and reload.
- Fix the code in your editor. Reload, because edits do not survive.
Common problems
| Symptom | Likely cause | What to do |
|---|---|---|
| Panel value changes but the screen does not | `OnPush` component, or the field is not tracked | Run `ng.applyChanges(cmp)`. Prefer signals for state that should drive the view. |
| Derived value does not update after editing an input | `ngOnChanges` did not run | Edit the derived value too, or change the parent's binding. |
| Value reverts right after your edit | A parent binding or a subscription overwrites it | Find the writer with a breakpoint, or edit the parent's value. |
| Cannot edit an object or function | The panel edits primitives only | Assign it from the console. |
| View source opens compiled JavaScript | Source maps are missing | Enable source maps in the build configuration. |
| `ng.getComponent($0)` returns `null` | `$0` is not a component host element | Select the host element, or use `ng.getOwningComponent($0)`. |
Exercise
You select a component that uses OnPush and edit its plain field errorMessage from "" to "Timeout" in the Properties panel. The panel shows "Timeout", but the error banner does not appear. Give two ways to make the banner show, and say which one you would choose if the field were meant to drive the view in the real app.
Show answer
First, in the console run const cmp = ng.getComponent($0); ng.applyChanges(cmp); to force Angular to refresh the component. Second, change the code so the state is a signal and set it with cmp.errorMessage.set('Timeout'), because a signal read in the template notifies Angular and needs no manual refresh. For a field that should drive the view in the real app, choose the signal: the forced refresh only works around the debugging session, while a signal fixes the underlying reason the plain field is not tracked under OnPush.
Exercise
After editing an input in the Properties panel, a derived label computed in ngOnChanges still shows the old text. Why, and how do you test the template with the new input?
Show answer
Editing in the panel writes the field directly. Angular did not process an input binding change, so ngOnChanges did not run. To test the template, either edit the derived label's field as well, or change the value in the parent component that binds to the input so Angular runs the normal input update and hooks.