Sign up ×
Stack Overflow is a community of 4.7 million programmers, just like you, helping each other. Join them; it only takes a minute:

I'm using angularjs-rails gem. I've created angular_app folder in assets, and have angular_app/controllers/phoneListController.js.coffee, and angular_app/modules/phoneCatApp.js.coffee (*yeah you're right I'm doing angular's phone tutorial ). So angular_app/controllers/phoneListController.js.coffee has:

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 has:

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

Every thing works fine if I use vanila js in angular_app/modules/phoneCatApp.js.coffee using `phonecatApp = angular.module('phonecatApp', [])`` (with backsticks).

So problem is that coffee covers all in anonymous function with ().call.this. What should I do to make it work in coffee?

share|improve this question
    
in angular_app/controllers/phoneListController.js.coffee it seems like you are not adding $scope as dependency. this line should be: phonecatApp.controller 'PhoneListController', '$scope', ($scope) -> ... – Jax Apr 1 at 8:55
    
Doesn't work: phonecatApp is not defined – ClassyPimp Apr 1 at 8:58
    
do you have an index.js.coffee file where you declare the files to be included in your project? – Jax Apr 1 at 9:02
    
they're in asset pipeline and served right (I can see them in chrome's dev panel), and everything works if i declare module in pure JS. – ClassyPimp Apr 1 at 9:11
1  
What I do is direct call angular.module('phonecatApp').controller ... with assets pipeline directive require <file with your module definition>. Local variables are not passed between coffee files. – BroiSatse Apr 1 at 9:11

1 Answer 1

up vote 0 down vote accepted

The problem, as you've stated, is the

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

that the coffeescript compiler generates. To make phonecatApp a global simply do

this.phonecatApp = phonecatApp

in your angular_app/modules/phoneCatApp.js.coffee file after the angular.module() call.

However, MUCH better is to use:

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

to define your controllers. This version gets angular to provide the module singleton on which to define the controller.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.