How to find a reason AngularJS Argument 'MyCtrl' is not a function, got undefined
- Is file with 'MyCtrl' connected via html? (check twice if you concat or uglify your files)
<script src='path/to/controllers.js'></script>
- Is 'MyCtrl' defined correctly?
app.controller('MyCtrl', ['$scope', function ($scope) {...}])
app.controller('MyCtrl', function ($scope) {...})
var MyCtrl = function ($scope) {...})
- Is 'MyCtrl' defined in right module?
- Is "MyCtrl's" module added to app dependencies?
angular.module('app', ['app.sources']);
If you define your module multiple times, you should define it in this order:
- First define should be like
angular.module('app.sources', []);
(with [ ]
)- Subsequent defines should be like
angular.module('app.sources');
(without [ ]
)Important: Declaration order is important - definition with [ ]
should go first.
- Check that module is defined only once.
You may have forgotten to rename module after copy-paste. Check src for string like
angular.module('app.sources', []);
Check your
'ng-app'
. Better to use only one of these with name likeng-app='app'
(In other words do not define multiple unnamed ngApp directives)Is your controller's syntax correct for your AngularJS version?
Error: [ng:areq] Argument 'MyCtrl' is not a function, got undefined
Turns out I had and the following html tag in the layout page which was returning this error
<html lang="en ng-app">
Removed the ng-app from the tag and code works perfectly Getting error - Argument 'myCtrl' is not a function, got undefined
The error has to do with how the app is initialised and the double ng-app
. You can easily verify that by bootstrapping angularjs manually:
When you remove both ng-app
and add the following snippet at the bottom at controllers/controller.js
, your script works:
angular.element(document).ready(function() {
angular.bootstrap(document, ['myApp']);
});
Argument '' is not a function got undefined angularjs
Here is your own code. Please go through it and for more just go to plunker
//JS code
var app = angular.module('myApp', [])
.controller('AppController', function($scope) {
$scope.data = 'Hello';
});
//template code
<div ng-app="myApp">
<div ng-controller="AppController">
<h1> {{data}}</h1>
</div>
</div>
Error: Argument is not a function, got undefined
Remove the []
from the name ([myApp]) of module
angular.module('myApp', [])
And add ng-app="myApp"
to the html and it should work. AngularJS: Argument 'Ctrl' is not a function, got undefined
As I know you need to define controller using module.controller method. For example, name your app as myApp
<html ng-app="myApp">
and the js part will be:angular.module('myApp', [])
.controller('Ctrl', ['$scope', function($scope) {
$scope.name = "Noob";
$scope.age = "21";
}]);
AngularJS: Cannot find the reason for the following error: [ng:areq] Argument 'MyCtrl' is not a function, got undefined
You need to define angular module
first then add that components like controller
, directive
, factory
, etc. in it, then you need to add that module inside the ng-app
directive.
Markup
<body ng-app="myApp">
<div ng-controller="MyCtrl">
<button ng-click="toggle()">Toggle</button>
<p ng-show="visible">Hello World!</p>
</div>
</body>
Codeangular.module('myApp', [])
.controller('MyCtrl', MyCtrl)
function MyCtrl($scope) {
$scope.visible = true;
$scope.toggle = function () {
$scope.visible = !$scope.visible;
};
};
Demo here
Related Topics
Keep Bootstrap Dropdown Open When Clicked Off
Single Plus Operator in JavaScript
Waiting for Image to Load in JavaScript
Catching Errors in JavaScript Promises with a First Level Try ... Catch
Using Raw Image Data from Ajax Request for Data Uri
Domnodeinserted Equivalent in Ie
React Doesn't Reload Component Data on Route Param Change or Query Change
JavaScript Object Literals Syntax Error
React-Router Getting This.Props.Location in Child Components
Validating User's Utf-8 Name in JavaScript
Promises for Promises That Are Yet to Be Created Without Using the Deferred [Anti]Pattern
Convert JavaScript Object or Array to JSON for Ajax Data
How to Find Out with Jquery If an Element Is Being Animated
Pass Parameter with Python Flask in External JavaScript
How to Stop Events Bubbling in Jquery
Using the Haversine Formula in JavaScript