错误:未知提供者:aProvider < – a
我在带有资产的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使用函数参数名来进行dependency injection。 所以是的,缩小确实打破了这一点。
但修复很简单。 在每个需要注入(使用’$ xxx’)变量的情况下,执行以下操作:
app.controller "TimesheetCtrl", ['$scope', ($scope) -> $scope.costed_amount = 0 ]
基本上,用数组替换所有函数定义。 最后一个元素应该是函数定义本身,第一个元素是要注入的对象的$names
。
关于文档还有一些(尽管不够清楚)信息。
如果您错过了某处的数组符号,为了找到它,我们需要稍微修改角度代码,但它的解决方案非常快。
更改是console.log(“数组表示法缺失”,fn); (从function开始的第11行)
找出angular.js中的注释函数(非缩小)
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 }; }]);