javascript - 如何在javascript代码中获取 Angular 服务的实例?

标签 javascript angularjs

我有一个 Angular 服务。这是代码:

angular.module('myServiceModule', [])
    .service('angService',function({
     this.get=function(){
        console.log('i am accessible');
     }
});

我必须在 javascript 代码中访问 Angular 服务的 get() 方法(而不是从 Angular 代码)。

<script>
  function caller(){
    //here i have to access service get() method.how is it possible.
    //can i do this->
    myServiceModule.get();
  }
 caller();//this function i am calling after service load.
</script>

我已经做到了。但这是不对的。怎么可能?

最佳答案

试试这个:

angular.injector(['ng', 'myServiceModule']).invoke(["myService", function(myService) {
    //do something with myService
}]);

关于javascript - 如何在javascript代码中获取 Angular 服务的实例?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29584324/

相关文章:

javascript - 多行 JSON 字符串到多行 Javascript 字符串

AngularJS UI 路由器 : Get list of child states of a given state

javascript - AngularJS orderBy 未按预期工作

javascript - 按下按钮时触发点击事件

javascript - GWT 书市或 GWT 作为外部图书馆

javascript - 使用javascript动态计算顶级属性

javascript - Jquery 数据表 - 页脚中的平均值

javascript - Angular 5 httpClient 版本的代码

angularjs - Ionic 应用程序在 Android 4 版本上显示白屏

AngularJS - 具有功能的克隆元素