how to debug angular services in browser

后端 未结 5 2059
感情败类
感情败类 2020-12-23 09:48

I have created a factory/service in my angular app.

I want to debug it in the browser. Is there a way that I can access its instance and check what is the value of

5条回答
  •  一向
    一向 (楼主)
    2020-12-23 10:23

    Like @bmleite said, you can use:

    angular.element( {DOM element} ).injector().get('serviceName')
    

    You can select the wanted DOM element (use 'Elements' tab on Chrome Developper Tools or 'HTML' tab on Firefox/Firebug) and then use $0 to point on it in the 'Console' tab.

    The simplest element to select is the one you have your ngApp directive on it. The final code is:

    var myService = angular.element($0).injector().get('serviceName');
    

    Then you have access to all his variables and functions:

    myService.getConfig('dev');
    

提交回复
热议问题