7

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

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

Error: Unknown provider: aProvider <- a

我已经将它追溯到我的咖啡脚本文件,我在其中设置了 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 资产编译和缩小造成的吗?有没有办法解决这个问题并仍然使用咖啡脚本和 Rails 资产?

4

3 回答 3

20

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

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

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

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

docs上还有更多(尽管不够清楚)信息。

于 2012-10-17T09:48:42.960 回答
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;
}
于 2014-04-17T10:00:37.943 回答
0

要缩小角度,您只需将声明更改为“数组”声明“模式”,例如:

从:

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
    };
}]);
于 2014-09-18T15:16:45.347 回答