Home > Article > Web Front-end > Why am I getting the `[$injector:modulerr]` error when migrating to AngularJS version 1.3?
Angularjs: Uncaught Error: [$injector:modulerr] in Migrating to Version 1.3
AngularJS version 1.3 introduced a change that broke the compatibility of code written for previous versions. In your case, you are encountering the [$injector:modulerr] error because you are declaring a global controller function (Ctrl) instead of registering it with an AngularJS module.
Defining AngularJS Modules
In AngularJS 1.3 and later, the preferred way to declare a controller is by registering it with an AngularJS module. Modules serve as a container for organizing and configuring AngularJS components like controllers, directives, services, and filters.
Resolving the Error
To resolve the error, you need to create an AngularJS module and register your Ctrl controller with it. Here's an updated version of your code:
<code class="javascript">function Ctrl($scope) { $scope.age = 24; } var myApp = angular.module('app', []); myApp.controller('Ctrl', ['$scope', Ctrl]);</code>
Upgrading to AngularJS 1.6
AngularJS 1.3 is now deprecated. It is recommended to upgrade to AngularJS version 1.6, which has a more stable API and improved performance. In AngularJS 1.6, the preferred way to declare a controller is through a class:
<code class="typescript">export class Ctrl { constructor(public $scope: ng.IScope) { this.$scope.age = 24; } }</code>
And register it with the AngularJS module:
<code class="javascript">myApp.controller('Ctrl', Ctrl);</code>
By following these guidelines, you can successfully migrate your AngularJS application to version 1.3 or upgrade to the latest version 1.6 and resolve the [$injector:modulerr] error.
The above is the detailed content of Why am I getting the `[$injector:modulerr]` error when migrating to AngularJS version 1.3?. For more information, please follow other related articles on the PHP Chinese website!