我使用的Ruby on Rails的3.2.8项目与资产AngularJS。
当我打开了我的形式,在使用我开发的机器上AngularJS我没有问题。 然而,当我加载相同的形式,我的生产服务器上,我得到在JavaScript控制台此错误:
Error: Unknown provider: aProvider <- a
我已经追踪它回到我的CoffeeScript文件表单中使用,我设置AngularJS:
$ (event) ->
$("#timesheet_description").autocomplete({source: '/autocomplete/work_descs'})
# Create AngularJS module
app = angular.module 'timesheetApp', []
# Create a AngularJS controller
app.controller "TimesheetCtrl", ($scope) ->
$scope.costed_amount = 0
# Bind my module to the global variables so I can use it.
angular.bootstrap document, ["timesheetApp"]
如果我评论这一切出来的页面将加载没有错误,没有AngularJS能力。
是由于Rails的资产编译和运行如下的问题? 有没有办法解决这个问题,仍然使用的CoffeeScript和Rails资产?
AngularJS,使用您现在正在使用(称为pretotyping)的风格时,使用该函数的参数名称做依赖注入。 所以,是的,缩小并彻底打破这一点。
解决方法是简单的,但。 在你需要注射(利用“$ XXX”)变量任何情况下,这样做:
app.controller "TimesheetCtrl", ['$scope', ($scope) ->
$scope.costed_amount = 0
]
基本上,代替以与阵列的所有函数定义。 最后一个元素应该是函数定义本身,而首当其冲是$names
你想注入的对象。
这里也有一些(虽然不够清晰)的信息的文档 。
如果你错过了数组符号的地方,找到这一点,我们需要修改角码点点,但是它非常快速的解决方案。
变化是的console.log(“数组符号被缺失”,FN); (从功能开始行号为11)
找出angular.js注释功能(非minified的)
function annotate(fn) {
var $inject,
fnText,
argDecl,
last;
if (typeof fn == 'function') {
if (!($inject = fn.$inject)) {
$inject = [];
if (fn.length) {
console.log("Array Notation is Missing",fn);
fnText = fn.toString().replace(STRIP_COMMENTS, '');
argDecl = fnText.match(FN_ARGS);
forEach(argDecl[1].split(FN_ARG_SPLIT), function(arg){
arg.replace(FN_ARG, function(all, underscore, name){
$inject.push(name);
});
});
}
fn.$inject = $inject;
}
} else if (isArray(fn)) {
last = fn.length - 1;
assertArgFn(fn[last], 'fn');
$inject = fn.slice(0, last);
} else {
assertArgFn(fn, 'fn', true);
}
return $inject;
}
来缩小角所有你需要的是做的是你的宣言更改为“阵列”申报“模式”,例如:
从:
var demoApp= angular.module('demoApp', []);
demoApp.controller(function demoCtrl($scope) {
} );
至
var demoApp= angular.module('demoApp', []);
demoApp.controller(["$scope",function demoCtrl($scope) {
}]);
如何申报工厂服务?
demoApp.factory('demoFactory', ['$q', '$http', function ($q, $http) {
return {
//some object
};
}]);