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

前端 未结 1 1421
误落风尘
误落风尘 2020-12-19 12:34

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

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

相关标签:
1条回答
  • 2020-12-19 13:23

    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);
    
    0 讨论(0)
提交回复
热议问题