Anthony Humes
to be able to debug any application, you have to understand the tools you can use
Comes directly from Ivy internal methods
// Global variable in the console
ng
{
getComponent: ƒ getComponent(element),
getContext: ƒ getContext(element),
getListeners: ƒ getListeners(element),
getOwningComponent: ƒ getOwningComponent(elementOrDir),
getHostElement: ƒ getHostElement(componentOrDirective),
getInjector: ƒ getInjector(elementOrDir),
getRootComponents: ƒ getRootComponents(elementOrDir),
getDirectives: ƒ getDirectives(element),
applyChanges: ƒ applyChanges(component)
}
$0
<p class="some-element"></p>
<div class="some-div">
<p class="some-element">
Hey there text!
</p>
</div>
most recent selection from the Elements Inspector or from using Inspect Element
retrieves the component from an HTML element
retrieves an array of directives from an HTML element
returns an array of both html (ex. click) and host listeners for an HTML element
<app-parent class="some-div">
<app-child class="some-element">
Hey there text!
</app-child>
</app-parent>
ng.getComponent($0)
ChildComponent {...}
<app-element class="some-div">
<p appExample appOther class="some-element">
Hey there text!
</p>
</app-parent>
ng.getDirectives($0)
[ExampleDirective {...}, OtherDirective {...}]
export class ExampleComponent {
@HostListener('mouseover', ['$event'])
onHover($event: MouseEvent) {
// some action on hover
}
}
ng.getListeners($0)
[{
element: app-example,
name: "mouseover",
callback: f wrapListenerIn_markDirtyAndPreventDefault(e),
useCapture: false,
type: "dom"
}]
<app-element class="some-div">
<p (click)="itemClicked($event)" class="some-element">
Hey there text!
</p>
</app-parent>
<app-element class="some-div">
<p (click)="itemClicked($event)" class="some-element">
Hey there text!
</p>
</app-parent>
ng.getListeners($0)
[{
element: p.some-element,
name: "click",
callback: f wrapListenerIn_markDirtyAndPreventDefault(e),
useCapture: false,
type: "dom"
}]
let component = ng.getComponent($0)
component.value = 10;
ng.applyChanges($0)
<app-element class="some-div">
<p class="some-element">
5
</p>
</app-parent>
<app-element class="some-div">
<p class="some-element">
10
</p>
</app-parent>
triggers change detection for the component or directive
<app-element class="some-div">
<p class="some-element">
Hey there text!
</p>
</app-parent>
ng.getOwningComponent($0)
ExampleComponent {...}
<ul>
<li><app-item></app-item></li>
<li><app-item></app-item></li>
<li><app-item></app-item></li>
<li><app-item></app-item></li>
<li><app-item></app-item></li>
</ul>
ng.getContext($0)
NgForOfContext {
first: false
last: false
even: true
odd: false
$implicit: {name: "Item 3", value: {…}}
ngForOf: (5) [{…}, {…}, {…}, {…}, {…}]
index: 2
count: 5
}
returns the parent component for the HTML element
returns the context of an *ngIf or *ngFor for the HTML element
Retrieves all root components associated with a DOM element, directive or component instance.
Root components are those which have been bootstrapped by Angular.
Retrieves an `Injector` associated with an element, component or directive instance.
The current API is a work in process. These methods are the starting point to a way more powerful set of debug tools than ng.probe. More methods and better functionality will be coming in the future.