YepMe YepMe - 4 years ago 92
AngularJS Question

"$injector:modulerr" : throwing error for module name is not available but actually it is available

I have just started learning angularjs I tried this code :
In the file angularmy.js

var myname = angular.module("myModule",[]);
myname.controller("myController" , function($scope){
$scope.message = "Hello lady";


<html >
<script src = "angular.js"></script>
<script src = "angularmy.js"></script>
<body ng-app = "myname" >
<div id="div1" ng-controller = "myController">
{{ message }}

It's throwing error saying Uncaught Error: [$injector:modulerr] Failed to instantiate module myname due to:
Error: [$injector:nomod] Module 'myname' 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.

But the module name is "myname" only, then why am I gettig the error ?

Answer Source

I think it is a typo. It need to be

var myname = angular.module("myname",[]);

Also note it is better to use Inline Array Annotation when injecting dependencies.

Else you may see this same error while running the application with minified code

//Note `[` brace
myname.controller("myController" ,['$scope, function($scope){ 
//Rest of code
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download