Home >Web Front-end >JS Tutorial >Why Am I Getting 'Controller not a function, got undefined' in AngularJS?
Global Controller Declaration Not Allowed
In modern versions of AngularJS (1.3 ), defining controllers globally on the global scope is no longer supported. The error message "Controller not a function, got undefined" indicates this issue.
To register controllers correctly, use the preferred module.controller syntax. Here are examples:
angular.module('app', []) .controller('ContactController', ['$scope', function ContactController($scope) { // Controller logic }]);
Or, using dependency injection:
function ContactController($scope) { // Controller logic } ContactController.$inject = ['$scope']; angular.module('app', []).controller('ContactController', ContactController);
Allowing Global Controllers
As a breaking change, global controller declaration has been disabled. However, this can be re-enabled by using allowGlobals.
angular.module('app') .config(['$controllerProvider', function($controllerProvider) { $controllerProvider.allowGlobals(); }]);
Additional Considerations
The above is the detailed content of Why Am I Getting 'Controller not a function, got undefined' in AngularJS?. For more information, please follow other related articles on the PHP Chinese website!