错误:未知提供者:aProvider <- a [英] Error: Unknown provider: aProvider <- a

查看:38
本文介绍了错误:未知提供者:aProvider <- a的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm using AngularJS in a Ruby on Rails 3.2.8 project with assets.

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

When I load up my form which is using AngularJS on my development machine I don't have a problem. However when I load the same form up on my production server I get this error in the Javascript console:

Error: Unknown provider: aProvider <- a

我已经将它追溯到我的咖啡脚本文件,我在该文件中设置了 AngularJS 以在表单中使用:

I've tracked it back to my coffeescript file where I setup AngularJS for use within a form:

$ (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 功能.

If I comment all this out the page will load without errors and without AngularJS abilities.

问题是由于 Rails 资产编译和缩小造成的吗?有没有办法解决这个问题并仍然使用咖啡脚本和 Rails 资产?

Is the problem due to Rails assets compiling and minify? Is there a way to fix this and still use coffeescript and Rails assets?

推荐答案

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

AngularJS, when using the style you're using right now (called pretotyping), uses the function argument names to do dependency injection. So yes, minification does break this completely.

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

The fix is simple, though. In every case where you need injection (are using '$xxx') variables, do this:

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

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

Basically, replace all function definitions with an array. The last element should be the function definition itself, and the first ones are the $names of the objects you want injected.

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

There's some more (albeit not clear enough) info on the docs.

这篇关于错误:未知提供者:aProvider &lt;- a的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆