我有一个 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/