我已经为此苦苦挣扎了几个小时,但没有找到好的解决方案。如何将对象注入(inject)到函数中,以便我可以在其中使用它?示例:
angular.module(moduleName).service('myService', MyService, MyObject);
到这里,MyObject已经定义好了,所以一切都OK了。但是,在函数内部,它是未定义的。如何将对象传递给函数并在其中使用它?
CamundaService.$inject = ['$http'];
function CamundaService($http, MyObject) {...}
将其作为数组中的第二个参数注入(inject)不起作用,并且出现错误:
angular.js:13708 Error: [$injector:unpr] Unknown provider
非常感谢任何帮助:)
最佳答案
您可以使用 Angular 常量
:
var app = angular.module('myApp', []);
app.constant('MyObject', {
message: 'hello world'
});
现在您可以注入(inject)MyObject
。
关于javascript - 将对象注入(inject)到服务函数中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38458158/