Home > Web Front-end > JS Tutorial > Why Am I Getting 'Controller not a function, got undefined' in AngularJS?

Why Am I Getting 'Controller not a function, got undefined' in AngularJS?

Susan Sarandon
Release: 2024-12-04 03:24:11
Original
556 people have browsed it

Why Am I Getting

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
    }]);
Copy after login

Or, using dependency injection:

function ContactController($scope) {
    // Controller logic
}
ContactController.$inject = ['$scope'];
angular.module('app', []).controller('ContactController', ContactController);
Copy after login

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();
    }]);
Copy after login

Additional Considerations

  • Ensure the correct angular root element includes the ng-app directive.
  • Verify the correct script files are included.
  • Avoid defining the same module multiple times, which may clear previously registered entities.

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!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template