Rails angular coffeescript 错误

Rails angular coffeescript error

我正在使用 angularjs-rails gem。 我在资产中创建了 angular_app 文件夹,并且有 angular_app/controllers/phoneListController.js.coffee 和 angular_app/modules/phoneCatApp.js.coffee(*是的,你是对的,我正在做angular 的 phone 教程)。所以 angular_app/controllers/phoneListController.js.coffee 有:

phonecatApp.controller 'PhoneListController', ($scope) ->
  $scope.phones = [
    { 'name': 'Nexus S'
      'snippet': 'Fast just got faster with Nexus S.'
    }
    {
      'name': 'Motorola XOOM™ with Wi-Fi'
      'snippet': 'The Next, Next Generation tablet.'
    }
    {
      'name': 'MOTOROLA XOOM™'
      'snippet': 'The Next, Next Generation tablet.'
    }
  ]
  return

angular_app/modules/phoneCatApp.js.coffee 有:

phonecatApp = angular.module('phonecatApp', [])

如果我在 angular_app/modules/phoneCatApp.js.coffee 中使用 vanila js 使用 `phonecatApp = angular.module('phonecatApp', [])``(带 backsticks),一切正常.

所以问题是咖啡用().call.this覆盖了匿名函数中的所有内容。我应该怎么做才能让它在咖啡中发挥作用?

正如您所说,问题是

(function() { ... }).call(this)

coffeescript 编译器生成的。要使 phonecatApp 成为全球性的,只需执行

this.phonecatApp = phonecatApp

angular_app/modules/phoneCatApp.js.coffee 文件中 angular.module() 调用后。

但是,更好的方法是使用:

angular.module('phonecatApp').controller( ... )

定义您的控制器。此版本获得 angular 以提供定义控制器的模块单例。