fuyi
fuyi

Reputation: 2639

angular module is not defined when writing in coffescript

I have defined a angular app module as below

var dashboardApp = angular.module('dashboardApp', []);

It works fine, but when I rewrite this module in coffeescript, angular report a error

dashboardApp is not defined

The coffeescript code is :

dashboardApp = angular.module 'dashboardApp', []

I know the when wring in coffeescript, all variable is automatically wrapped to avoid polluting global scope.

So the question is: Does this mean angular require module to be defined in global scope? is it possible to use coffeescript to define the module?

Update

The accurate error message is

Uncaught ReferenceError: dashboardApp is not defined

I got this error because I use dashboardApp to create controller and service like this in other files:

dashboardApp.factory 'Utility', ->
Utility = {}
Utility.imageValidator = (file, max_size,allowed_type) ->
    file_type = file.type
    file_size = file.size
    result = true
    result = false if file_size > max_size
    result = false if ( allowed_type.indexOf(file_type) is -1)
    result

Utility

Upvotes: 0

Views: 383

Answers (1)

Patrick
Patrick

Reputation: 861

The error you're experiencing is a JavaScript error, not an Angular one. You have a couple options.

  • Use the module dependency pattern
  • Combine the files
  • Explicitly put dashboardApp in global scope

I suggest the module dependency pattern as it explicitly shows what modules depend on what other functionality.

App File

dashboardApp = angular.module 'dashboardApp', ['dashboardApp.factories']

Factory File

dashboardAppFactories = angular.module 'dashboardAppFactories, []
dashboardAppFactories.factory 'Utility' , => ...

Upvotes: 1

Related Questions