3

This is surely a noob question but I just can't seem to figure out what is wrong with this simple module initiation (JSFiddle):

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

I get an error saying that "Module 'myApp' is not available!". Does anyone know why this does not work?

Stig Perez
  • 3,595
  • 4
  • 23
  • 36

1 Answers1

1

It's because (in the fiddle at least) that the script is run on window.onload, thus angular cannot find the module before it sees in the DOM that there should be a module named myApp.

before it was (in the head):

window.onload = function() {
    angular.module(...)
}

but it had to be:

angular.module(...)

ie. not waiting until the document was fully loaded, thus creating the module before angular sees that it should bootstrap the module myApp.

updated fiddle

Björn Roberg
  • 2,275
  • 2
  • 15
  • 15
  • You're right, that did fix the jsfiddle one. And I also found out why my offline example did not work. For some reason a Notepad++ plugin decided to format my HTML, including the reference to my script file where I put the module initialization. For some insane reason the plugin thought that was better than – Stig Perez Nov 15 '13 at 13:57
  • @boomshanka you should file a bug report to the maintainers of that plugin :) HTH – Björn Roberg Nov 15 '13 at 13:58
  • Yeah that would be the right thing to do - either that or get myself a decent angular IDE :-) – Stig Perez Nov 15 '13 at 14:39
  • Not specific to this particular case but may help some (this post appears pretty high on google for this issue search): if your module is in a separate file, it also has to be included in the html: – timhc22 Sep 30 '14 at 23:36