0

I have done a test app in AngularJS, now I'm trying to a little different and with coffee. The thing is, that it gives me an error like this:

Error: [$injector:modulerr] Failed to instantiate module app due to:
Error: [$injector:nomod] Module 'app' is not available! You either misspelled the module name or forgot to load it. If registering a module ensure that you specify the dependencies as the second argument. 

And I don't understand, why it doesn't see my app module. Here's my code:


index.html

<!doctype html>
<html lang="en" ng-app="app">
  <head>
    <meta charset="utf-8">
    <link rel="stylesheet" href="bower_components/bootstrap/dist/css/bootstrap.css">
    <link rel="stylesheet" href="css/app.css">

    <script src="bower_components/jquery/jquery.js"></script>
    <script src="bower_components/angular/angular.js"></script>
    <script src="bower_components/angular-animate/angular-animate.js"></script>
    <script src="bower_components/angular-route/angular-route.js"></script>
    <script src="bower_components/angular-resource/angular-resource.js"></script>

    <script src="coffee/app.coffee" type="text/coffeescript" ></script>
    <script src="coffee/controllers.coffee" type="text/coffeescript"></script>
  </head>
  <body>

    <div class="view-container">
      <div ng-view></div>
    </div>

  </body>
</html>

app.coffee

app = angular.module 'app', [
  'ngRoute'
  'commentController'
]

app.config [ '$routeProvider',
  ($routeProvider) ->
    $routeProvider.
      when('/title', {
          templateUrl: 'templates/title.html'
          controller: 'CommentListCtrl'
        }).
      otherwise({
        redirectTo: '/title'
      })
]

And controllers.coffee:

commentController = angular.module 'commentController', [] 

commentController.controller 'CommentListCtrl', [ '$scope',
  ($scope) ->
    $scope.hello = "HELLO!"
]
1
  • Well I did it another way. Installed yeoman(cause earlier I didn't know about this thing) and delegated him to do all for me :)But still thx for your answer.You can add it to your answer so I'll accept it as full answer :) Commented Nov 26, 2014 at 9:57

1 Answer 1

1

Coffeescript is not natively supported by browsers: you have to compile it into Javascript.

You can do it "easily" by using tasks runners such as Grunt or Gulp and appropriates plugins (gulp-coffee, grunt-contrib-coffee), or with Browserify and appropriates transformations (coffeeify). Yeoman uses Grunt/Gulp.

Now, about the error:

<script src="coffee/app.coffee" type="text/coffeescript" ></script>

This snippet will effectively load the script but wont interpret it. You can get its content and manipulate it as a string (that's what is used by some templating libraries), but that's it. The browser won't tell you "Hey, I can't read this, it's coffeescript".

After the page being loaded, Angular, that was successfully loaded as it is plain javascript, will parse your DOM, read ng-app="app", and try to see if he knows the module app. Unfortunately, your scripts were not interpreted, thereby the app module not declared: that's the error you get !

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.