Home  >  Article  >  Web Front-end  >  How to Fix \"Uncaught Error: [$injector:modulerr]\" When Migrating to AngularJS v1.3?

How to Fix \"Uncaught Error: [$injector:modulerr]\" When Migrating to AngularJS v1.3?

Susan Sarandon
Susan SarandonOriginal
2024-11-01 04:13:27702browse

How to Fix

AngularJS: Uncaught Error: [$injector:modulerr] When Migrating to V1.3

In AngularJS version 1.3, global controller function declaration is no longer permitted. Instead, developers must create an AngularJS module and attach components to it.

To resolve the error, follow these steps:

  • Create an AngularJS module using angular.module('app', []).
  • Attach the controller to the module using .controller('Ctrl', ['$scope', Ctrl]).

For example:

<code class="js">function Ctrl($scope) {
    $scope.age = 24;
}

angular.module('app', [])
    .controller('Ctrl', ['$scope', Ctrl]);</code>

Note: AngularJS version 1.3.14 has some issues with this approach, so consider downgrading to version 1.3.13 or using AngularJS 1.6.X for a more stable experience.

Plunkr Example (AngularJS 1.3.13):

https://plnkr.co/edit/Ei7P5xJ5NCKz9UEFyWij

Alternative Solution:

If you prefer to continue using global controller declaration, you can allow it within angular.config, although this is not the recommended approach:

<code class="js">angular.module('app', [])
    .config(['$controllerProvider',
        function ($controllerProvider) {
            $controllerProvider.allowGlobals();
        }
    ]);</code>

The above is the detailed content of How to Fix \"Uncaught Error: [$injector:modulerr]\" When Migrating to AngularJS v1.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