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:

Trying to convert working JS to coffee script in my angular app but its raises Error: [ng:areq] Argument 'ContactController' is not a function, got undefined

Here is my code.

angular.module("app", [
  "ngResource"
  "ngRoute"
]).run ($rootScope) ->

$rootScope.log = (thing) ->
console.log thing

The following js works fine

angular.module("app", ["ngResource", "ngRoute"]).run(function($rootScope) {
  $rootScope.log = function(thing) {
    console.log(thing);
  };
});
share|improve this question
    
Try using js2coffee.org to convert for you – Steve Mar 27 '14 at 1:04

2 Answers 2

up vote 1 down vote accepted

Your indents are off. Coffeescript is whitespace aware.

angular.module("app", [
  "ngResource"
  "ngRoute"
]).run ($rootScope) ->    
  $rootScope.log = (thing) ->
    console.log thing

Becomes:

angular.module("app", [ "ngResource", "ngRoute" ]).run ($rootScope) ->
  $rootScope.log = (thing) ->
    console.log thing

This doesn't explain why ContactController wouldn't be loading, but if your module isn't being defined correctly that could explain it.

share|improve this answer
    
same issue. Here is how I declare my controller angular.module("app").controller "ProductsController", ($scope, $http) -> It works fine with plain JS, bummer. – olivier Mar 26 '14 at 22:26
    
You'll want to get in the practice of named parameters for angular: module.controller("personList", ["$scope", "toolsService", 'idService', ($scope, tools, idService) -> ... etc ]) . I don't think that's the issue here. Can you put this up on plnkr.co? That's the easiest way to troubleshoot angular issues. – jcollum Mar 26 '14 at 22:31
    
Here you go plnkr.co/edit/bYFVnbx0ApFLzJpnBCSr – olivier Mar 26 '14 at 22:56
    
I cheated and grabbed an existing template, but I adapted it to your code: plnkr.co/edit/AzCz8SDpL23qujrp5dQs?p=preview -- in the future you should get in the practice of putting things like logging into a service instead of putting them on the rootscope. I use a toolsService that has lodash and my app config, for instance. – jcollum Mar 26 '14 at 23:09
    
Thanks @jcollum solved – olivier Mar 27 '14 at 0:19
angular.module("app", [
  "ngResource"
  "ngRoute"
]).run ($rootScope) ->

You missed a comma here..

angular.module("app", [
  "ngResource",
  "ngRoute"
]).run ($rootScope) ->
share|improve this answer
    
Nope, coffeescript will interpret same-level indents as separate items if they are in an array or function arg declaration. That compiles to: angular.module("app", ["ngResource", "ngRoute"]).run(function($rootScope) {}); – jcollum Mar 26 '14 at 22:22
1  
Thanks, I didn't know that. :) – Diunuge Mar 27 '14 at 10:23

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.