此 CoffeeScript 代码正在尝试创建一个 Angular 提供程序,但我收到此消息:提供程序“ItemsProvider”必须定义 $get 工厂方法。
我设置了 $get 方法。知道发生了什么吗?
'use strict'
app = angular.module('logica-erp')
app.provider 'ItemsProvider', [ ->
this.$get = ->
return {
}
]
加载失败并显示以下消息:
Error: [$injector:modulerr] Failed to instantiate module logica-erp due to:
[$injector:pget] Provider 'ItemsProvider' must define $get factory method.
编辑: 这是生成的 JavaScript:
(function() {
'use strict';
var app;
app = angular.module('logica-erp');
app.provider('ItemsProvider', [
function() {
return this.$get = function() {
return {};
};
}
]);
}).call(this);
最佳答案
CoffeeScript 引入了语法糖衣,读者和专家都可能很难理解。将其编译为 JS 看看发生了什么总是一个好主意。隐性返回似乎是我实践中最大的麻烦制造者。
在这种情况下,CS 代码被编译为这样
app.provider('ItemsProvider', [
function() {
return this.$get = function() {
return {};
};
}
]);
此处提供程序构造函数返回 this.$get
值(函数),而不是 this
对象。构造函数不应返回任何内容(除了极少数情况下应返回的内容):
app.provider('ItemsProvider', [
function() {
this.$get = function() {
return {};
};
}
]);
小心箭头。
关于javascript - Angular 提供者错误 - 必须定义 $get 工厂方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38130968/