Error in AngularJS: Controller Registration
Question:
When attempting to define controllers globally in an AngularJS application, the error "Controller not a function, got undefined" occurs.
Answer:
As of AngularJS 1.3 , global controller declaration is no longer supported. Controllers must now be registered using the module.controller syntax.
Registration Methods:
angular.module('app', []).controller('ContactController', ['$scope', function($scope) { // Controller logic }]);
function ContactController($scope) { // Controller logic } ContactController.$inject = ['$scope']; angular.module('app', []).controller('ContactController', ContactController);
Alternative: Allow Globals
The use of global controllers can be re-enabled by setting allowGlobals in the $controllerProvider:
angular.module('app').config(['$controllerProvider', function($controllerProvider) { $controllerProvider.allowGlobals(); }]);
Additional Considerations:
The above is the detailed content of Why is my AngularJS Controller undefined, and how can I register it correctly?. For more information, please follow other related articles on the PHP Chinese website!