Home >Web Front-end >JS Tutorial >How to Fix 'Argument 'ContactController' is not a function' Errors in Angular 1.3 ?

How to Fix 'Argument 'ContactController' is not a function' Errors in Angular 1.3 ?

Linda Hamilton
Linda HamiltonOriginal
2024-12-01 21:56:14494browse

How to Fix

Issue: Global Controller Declaration Error

Angular 1.3 has deprecated the use of global controller declarations on the global scope. This issue manifests as an error stating "Argument 'ContactController' is not a function, got undefined," preventing controllers from being defined globally without explicit registration.

Solution: Register Controllers with Module Syntax

To fix this, controllers should be registered using the module.controller syntax. For example:

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

Alternatively, you can inject the controller as a function:

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

Turning on Globals

If you prefer to use global declarations, you can enable them by setting allowGlobals in the $controllerProvider.

angular.module('app')
  .config(['$controllerProvider', function($controllerProvider) {
    $controllerProvider.allowGlobals();
  }]);

Additional Notes

  • Ensure that your app name is included in the ng-app directive on the root element (e.g., ng-app="myApp").
  • Verify that the correct scripts are included.
  • Avoid defining the same module twice in different locations, as this can overwrite previously registered entities.

The above is the detailed content of How to Fix 'Argument 'ContactController' is not a function' Errors in Angular 1.3 ?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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