Using AngularJS with Scala Play, I\'m getting this error.
Error: Argument \'MainCtrl\' is not a function, got undefined
I\'m try
Some times this error is a result of two ng-app
directives specified in the html.
In my case by mistake I had specified ng-app
in my html
tag and ng-app="myApp"
in the body
tag like this:
<html ng-app>
<body ng-app="myApp"></body>
</html>
Turns out it's the Cache of the browser, using Chrome here. Simply check the "Disable cache" under Inspect (Element) solved my problem.
FIRST.
check if you have correct controller
in the route definitions, same as the controller names that you are defining
communityMod.config(['$routeProvider',
function($routeProvider) {
$routeProvider.
when('/members', {
templateUrl: 'modules/community/views/members.html',
controller: 'CommunityMembersCtrl'
}).
otherwise({
redirectTo: '/members'
});
}]);
communityMod.controller('CommunityMembersCtrl', ['$scope',
function ($scope) {
$scope.name = 'Hello community';
}]);
different controller names in this example will lead to errors, but this example is correct
SECOND check if you have imported your javascript file:
<script src="modules/community/controllers/CommunityMembersCtrl.js"></script>
I had the same error message (in my case : "Argument 'languageSelectorCtrl' is not a function, got undefined").
After some tedious comparison with Angular seed's code, I found out that I had previously removed a reference to the controllers module in app.js. (spot it at https://github.com/angular/angular-seed/blob/master/app/js/app.js)
So I had this:
angular.module('MyApp', ['MyApp.filters', 'MyApp.services', 'MyApp.directives'])
This failed.
And when I added the missing reference:
angular.module('MyApp', ['MyApp.filters', 'MyApp.services', 'MyApp.controllers', 'MyApp.directives'])
The error message disappeared and Angular could instanciate the controllers again.
I have encountered the same problem and in my case it was happening as a result of this problem:
I had the controllers defined in a separate module (called 'myApp.controllers') and injected to the main app module (called 'myApp') like this:
angular.module('myApp', ['myApp.controllers'])
A colleague pushed another controller module in a separate file but with the exact same name as mine (i.e. 'myApp.controllers' ) which caused this error. I think because Angular got confused between those controller modules. However the error message was not very helpful in discovering what is going wrong.
In my case (having an overview page and an "add" page) I got this with my routing setup like below. It was giving the message for the AddCtrl that could not be injected...
$routeProvider.
when('/', {
redirectTo: '/overview'
}).
when('/overview', {
templateUrl: 'partials/overview.html',
controller: 'OverviewCtrl'
}).
when('/add', {
templateUrl: 'partials/add.html',
controller: 'AddCtrl'
}).
otherwise({
redirectTo: '/overview'
});
Because of the when('/'
route all my routes went to the overview and the controller could not be matched on the /add route page rendering. This was confusing because I DID see the add.html template but its controller was nowhere to be found.
Removing the '/'-route when case fixed this issue for me.