Angular JS Uncaught Error: [$injector:modulerr]
JavascriptAngularjsRoutesJavascript Problem Overview
I am having a problem with Angular JS receiving an error : Uncaught Error: [$injector:modulerr]. My JS-file looks
angular.module('MyApp', ['ngRoute']);
angular.module('MyApp',['ngResource']);
function TwitterCtrl($scope,$resource){
}
I also included angular-route-js
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.7/angular.min.js">
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.7/angular-route.min.js">
Angular documentation says the problem is http://docs.angularjs.org/api/ngRoute
Javascript Solutions
Solution 1 - Javascript
In development environments I recommend you to use not minified distributives. And all errors become more informative! Instead of angular.min.js
, use angular.js
.
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.7/angular.js">
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.7/angular-route.js">
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.7/angular-resource.js">
Solution 2 - Javascript
Try adding this:
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.7/angular-resource.min.js"></script>
Solution 3 - Javascript
Try adding:
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.7/angular-resource.min.js">
and:
angular.module('MyApp', ['ngRoute','ngResource']);
function TwitterCtrl($scope,$resource){
}
You should call angular.module
only once with all dependencies because with your current code, you're creating a new MyApp
module overwriting the previous one.
From angular documentation:
> Beware that using angular.module('myModule', []) will create the > module myModule and overwrite any existing module named myModule. Use > angular.module('myModule') to retrieve an existing module.
Solution 4 - Javascript
Make sure you're function is wrapped in a closure, complete with the extra () at the end:
(function(){
var app = angular.module('myApp', []);
})();
Solution 5 - Javascript
I previously had the same issue, but I realized that I didn't include the "app.js
" (the main application) inside my main page (index.html
).
So even when you include all the dependencies required by AngularJS, you might end up with that error in the console. So always make sure to include the necessary files inside your main page and you shouldn't have that issue.
Hope this helps.
Solution 6 - Javascript
The problem was caused by missing inclusion of ngRoute module. Since version 1.1.6 it's a separate part:
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.0rc1/angular-route.min.js"></script>
var app = angular.module('myapp', ['ngRoute']);
This is getting reference from: https://stackoverflow.com/questions/18287482/angularjs-1-2-injectormodulerr David answer
Solution 7 - Javascript
I had the same problem. You should type your Angular js code outside of any function like this:
$( document ).ready(function() {});
Solution 8 - Javascript
I got this error because I had a dependency on another module that was not loaded.
> angular.module("app", ["kendo.directives"]).controller("MyCtrl", function(){}...
so even though I had all the Angular modules, I didn't have the kendo one.
Solution 9 - Javascript
ok if you are getting a Uncaught Error: [$injector:modulerr]
and the angular module is in the error that is telling you, you have an duplicate ng-app module.
Solution 10 - Javascript
Make sure that the variable that holds your angular.module is structured correctly.
This will fail with "Uncaught Error: [$injector:modulerr]":
var angApp = angular.module("angApp");
This works:
var angApp = angular.module("angApp", []);
It's a sneaky one since the error message doesn't point to one thing in particular (Thus the wide variety of answers). Additionally, most js linters won't catch the particulars. Keep at it!
Solution 11 - Javascript
I had exactly the same problem and what resolved it was to remove the closure:
$(function(){
var app = angular.module("myApp", []);
app.controller('myController', function(){
...
});
});
becomes:
var app = angular.module("myApp", []);
app.controller('myController', function(){
...
});
Solution 12 - Javascript
The error means that the dependency injector was unable to locate the dependency 'ngResource'. The script tag in the accepted answer provides this dependency.
You will also get the same error if you add any custom modules in the dependencies but did not add the script tag for including the '.js' file containing the dependency.
Solution 13 - Javascript
Just throwing this in in case it helps, I had this issue and the reason for me was because when I bundled my Angular stuff I referenced the main app file as "AngularWebApp" instead of "AngularWebApp.js", hope this helps.
Solution 14 - Javascript
I had also same issue, I have just removed following line of code from BundleConfig.cs file and my code is working fine.
BundleTable.EnableOptimizations = true;
Solution 15 - Javascript
Do not load the javascript inside the cdn link script tag.Use a separate script tag for loading the AngularJs scripts.
I had the same issue but I created a separate <script>
Then the error gone.