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

map*_*ap7 7 ruby-on-rails coffeescript ruby-on-rails-3 angularjs

我在带有资产的Ruby on Rails 3.2.8项目中使用AngularJS.

当我加载在我的开发机器上使用AngularJS的表单时,我没有问题.但是,当我在生产服务器上加载相同的表单时,我在Javascript控制台中收到此错误:

Error: Unknown provider: aProvider <- a
Run Code Online (Sandbox Code Playgroud)

我已经将它追溯到我的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"]  
Run Code Online (Sandbox Code Playgroud)

如果我评论所有这一切,页面将加载没有错误和没有AngularJS能力.

问题是由于Rails资产编译和缩小?有没有办法解决这个问题,仍然使用coffeescript和Rails资产?

Sud*_*han 20

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

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

app.controller "TimesheetCtrl", ['$scope', ($scope) ->
  $scope.costed_amount = 0
]
Run Code Online (Sandbox Code Playgroud)

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

关于文档还有一些(尽管不够清楚)信息.

  • 并且你可以使用伟大的,非常好的ng-min模块来处理这个问题.egghead.io上的解释非常好:https://egghead.io/lessons/angularjs-ngmin (2认同)

Dee*_*til 6

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

更改是console.log("数组表示法缺失",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;
}
Run Code Online (Sandbox Code Playgroud)