How do I access the directive instance in angular from console?
Asked Answered
D

1

5

ng.probe($0).componentInstance -- will give the reference to the instance.

Is there any way to access the directive instance from the console?

Dough answered 8/2, 2018 at 12:42 Comment(0)
T
6

Directive instance doesn't differ from other providers in this respect, it can be retrieved from injector.

In order to retrieve provider instance, it is necessary to have provider token, which is directive class. Since application classes aren't exposed to global scope, debug element providerTokens can be inspected.

Given the application is unminified and functions preserve their original names, directive (provider) token is retrieved like:

var FooDirective = ng.probe($0).providerTokens.find(token => token.name === 'FooDirective');

And directive (provider) instance is retrieved like:

ng.probe($0).injector.get(FooDirective);
Tryptophan answered 8/2, 2018 at 14:6 Comment(1)
very impressive!Melonie

© 2022 - 2024 — McMap. All rights reserved.