How can I test an AngularJS service from the console?

前端 未结 4 1339
滥情空心
滥情空心 2020-11-29 14:06

I have a service like:

angular.module(\'app\').factory(\'ExampleService\', function(){
  this.f1 = function(world){
    return \'Hello \'+world;
  }
  return         


        
4条回答
  •  眼角桃花
    2020-11-29 14:50

    Angularjs Dependency Injection framework is responsible for injecting the dependancies of you app module to your controllers. This is possible through its injector.

    You need to first identify the ng-app and get the associated injector. The below query works to find your ng-app in the DOM and retrieve the injector.

    angular.element('*[ng-app]').injector()
    

    In chrome, however, you can point to target ng-app as shown below. and use the $0 hack and issue angular.element($0).injector()

    Once you have the injector, get any dependency injected service as below

    injector = angular.element($0).injector();
    injector.get('$mdToast');
    

提交回复
热议问题