Error: [ng:areq] from angular controller

Solution 1:

I experienced this error once. The problem was I had defined angular.module() in two places with different arguments.

Eg:

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

in other place,

var MyApp2 = angular.module('MyApp', ['ngAnimate']);

Solution 2:

I've gotten that error twice:

1) When I wrote:

var app = module('flapperNews', []);

instead of:

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

2) When I copy and pasted some html, and the controller name in the html did not exactly match the controller name in my app.js file, for instance:

index.html:

<script src="app.js"></script>
...
...
<body ng-app="flapperNews" ng-controller="MainCtrl">

app.js:

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

app.controller('MyCtrl', ....

In the html, the controller name is "MainCtrl", and in the js I used the name "MyCtrl".

There is actually an error message embedded in the error url:

Error: [ng:areq] http://errors.angularjs.org/1.3.2/ng/areq?p0=MainCtrl&p1=not%20a%20function%2C%20got%20undefined

Here it is without the hieroglyphics:

MainCtrl not a function got undefined

In other words, "There is no function named MainCtrl. Check your spelling."

Solution 3:

I ran into this issue when I had defined the module in the Angular controller but neglected to set the app name in my HTML file. For example:

<html ng-app>

instead of the correct:

<html ng-app="myApp">

when I had defined something like:

angular.module('myApp', []).controller(...

and referenced it in my HTML file.

Solution 4:

you forgot to include the controller in your index.html. The controller doesn't exist.

<script src="js/controllers/Controller.js"></script>