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?

1 Answer 1

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

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

4 Comments

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 <script src... /> was better than <script src...></script>
@boomshanka you should file a bug report to the maintainers of that plugin :) HTH
Yeah that would be the right thing to do - either that or get myself a decent angular IDE :-)
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: <script src="js/services.js"></script>

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.