YepMe YepMe - 4 months ago 9
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";
});


In HTML :

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


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

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
}]);