How can I test an AngularJS service from the console?
问题 I have a service like: angular.module(\'app\').factory(\'ExampleService\', function(){ this.f1 = function(world){ return \'Hello \'+world; } return this; }) I would like to test it from the JavaScript console and call the function f1() of the service. How can I do that? 回答1: TLDR: In one line the command you are looking for: angular.element(document.body).injector().get('serviceName') Deep dive AngularJS uses Dependency Injection (DI) to inject services/factories into your components