gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 21:50:23 24 4
gpt4 key购买 nike

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

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com