错误:未知提供者:aProvider< - a

时间:2012-10-17 04:29:19

标签: ruby-on-rails ruby-on-rails-3 coffeescript angularjs

我在带有资产的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资产?

3 个答案:

答案 0 :(得分:20)

当AngularJS使用你现在使用的样式(称为预处理)时,使用函数参数名来进行依赖注入。所以是的,缩小确实打破了这一点。

但修复很简单。在您需要注入(使用'$ xxx')变量的每种情况下,请执行以下操作:

app.controller "TimesheetCtrl", ['$scope', ($scope) ->
  $scope.costed_amount = 0
]

基本上,用数组替换所有函数定义。最后一个元素应该是函数定义本身,第一个元素是要注入的对象的$names

docs还有一些(尽管不够清楚)信息。

答案 1 :(得分:6)

如果你错过了某处的数组符号,为了找到它,我们需要稍微修改角度代码,但它的解决方案很快。

更改是 console.log(&#34;数组表示法缺失&#34;,fn); (从功能开始的第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;
}

答案 2 :(得分:0)

要缩小角度,您需要做的就是将声明更改为&#34;数组&#34;声明&#34;模式&#34;例如:

发件人:

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
    };
}]);