0

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
2
  • More information is needed to answer this. The error for angular not being able to find a module is more like 'module dashboardApp is not available'. What you've posted is more like a JavaScript error. Are you sure you're not trying to reference dashboardApp somewhere else you're expecting it to be in global scope? Commented Apr 28, 2014 at 20:53
  • I have updated the question with more information Commented Apr 28, 2014 at 21:09

1 Answer 1

1

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' , => ...
Sign up to request clarification or add additional context in comments.

1 Comment

I think the module dependency pattern is the one I am looking for. thanks

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.