Sub topic 11
Combining DevTools with the console's ng utilities
This chapter assumes you can use the Angular DevTools component explorer and the browser's Elements and Console panels. It also assumes your app runs in development mode. The ng utilities are not available in production builds.
What the ng global is
In development mode, Angular adds a global object named ng to the page. Its functions take a DOM element or a component instance and return Angular information about it. You can call them from the Console panel or from a script you paste there.
The DevTools panels are best for browsing. The console is better when you want to script, compare or log values. Use both on the same page.
Connecting the two tools
The Elements panel stores the element you selected in the variable $0. Most ng functions accept that element. So the usual path is to select an element and then ask Angular what it knows about it.
| Function | What it returns |
|---|---|
| `ng.getComponent(el)` | The component instance that owns the element |
| `ng.getOwningComponent(el)` | The component whose template contains the element |
| `ng.getHostElement(cmp)` | The DOM element that hosts a component or directive |
| `ng.getDirectives(el)` | The directive instances on the element |
| `ng.getListeners(el)` | The event listeners Angular registered on the element |
| `ng.getInjector(el)` | The injector the element resolves dependencies from |
| `ng.getRootComponents(el)` | The root components of the app that contains the element |
| `ng.applyChanges(cmp)` | Runs change detection after you change a property |
Reading and changing state
The first console example selects an element and reads its component. It then changes a property and re-renders.
// 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);The applyChanges call matters. Setting cmp.title only changes a field on an object. Angular does not notice the change until a change detection pass runs. With OnPush components, no pass runs for that component unless something marks it dirty. ng.applyChanges marks the component and runs change detection, so the view updates.
The DevTools property editor does the same job with less typing. The console is more convenient when you want to change several values in a row or loop over them.
Looking at wiring
Two other functions are useful when a click does nothing or a service seems wrong.
// Select the button in the Elements panel first.
console.log(ng.getListeners($0)); // which events does Angular handle here?
console.log(ng.getDirectives($0)); // which directives sit on this element?
console.log(ng.getInjector($0)); // where do its dependencies come from?If getListeners returns an empty array for an element, the template binding is missing or was never created. Check whether the binding sits inside an @if block that is currently false.
Common errors
ng is not defined. The app is a production build, or the page has no Angular app. Serve it with the development configuration.ng.getComponent($0)returnsnull. The selected element is not inside a component's view. Select an element within the component's template.- The view does not update after an edit. You skipped
ng.applyChanges(cmp). $0points to the wrong element.$0is whatever you last selected in the Elements panel, not in the DevTools tree. Reselect it first.- The change disappears on reload. Console edits change only the running instance. You still have to fix the source code.
Exercise
You selected a button in the Elements panel. You ran ng.getComponent($0).disabled = false, but the button on screen is still disabled. The component uses OnPush. What is missing, and what would you call to inspect whether the button's click handler is registered?
Show answer
The component has not been told to re-render. Call ng.applyChanges(ng.getComponent($0)) so Angular marks the component and runs change detection. To check the click handler, call ng.getListeners($0) and look for an entry with the name click.