Angularjs Uncaught Error:[$ injector:modulerr]迁移到V1.3时 [英] Angularjs Uncaught Error: [$injector:modulerr] when migrating to V1.3

查看:218
本文介绍了Angularjs Uncaught Error:[$ injector:modulerr]迁移到V1.3时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在学习Angular.js,我无法弄清楚这个简单的代码有什么问题。它似乎看起来很好,但给我跟随错误。

I am learning Angular.js and I am not able to figure out whats wrong with this simple code. It seems to look fine but giving me following error.

**Error**: Uncaught Error: [$injector:modulerr] http://errors.angularjs.org/1.3.14/$injector/modulerr?p0=app&p1=Error%3A%20…gleapis.com%2Fajax%2Flibs%2Fangularjs%2F1.3.14%2Fangular.min.js%3A17%3A381)

在添加之前ng-app = app (我只是将其保留为 ng-app )它给了我以下错误。为什么?

And before adding ng-app="app" (I was just keeping it as ng-app) it was giving me following errors. Why is that?

Error: [ng:areq] http://errors.angularjs.org/1.3.14/ng/areq?p0=Ctrl&p1=not%20a%20function%2C%20got%20undefined
    at Error (native)
    at https://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js:6:417
    at Sb (https://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js:19:510)
    at tb (https://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js:20:78)
    at $get (https://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js:75:331)
    at https://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js:57:65
    at s (https://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js:7:408)
    at A (https://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js:56:443)
    at g (https://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js:51:299)
    at g (https://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js:51:316)



<!doctype html>
    <html ng-app="app">
      <head>
        <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js"></script>

      </head>
      <body>
        <div ng-controller="Ctrl">
          <input ng-model="name">
          <h1>{{name}}</h1>
          <h2>{{age}}</h2>
        </div>

         <script>
          var Ctrl = function($scope)
           {
              $scope.age = 24;
           };
          </script>


      </body>
    </html>


推荐答案

AngularJS版本1.3全局控制器函数声明被禁用后

After AngularJS version 1.3 global controller function declaration is disabled

您需要先创建一个AngularJS模块&然后将所有组件附加到该特定模块。

You need to first create an AngularJS module & then attach all the components to that specific module.

代码

function Ctrl($scope) {
    $scope.age = 24;
}

angular.module('app', [])
    .controller('Ctrl', ['$scope', Ctrl]);

特别针对您的情况,AngularJS存在一些问题 1.3 .14 (将其降级为 1.3.13 正常工作)。虽然我更喜欢你使用 angular 1.2.27 AngularJS 1.6.X,这是更稳定的版本&最新版本的AngularJS。

Specifically for your case, there is some issue with AngularJS 1.3.14 (downgrade it to 1.3.13 works fine). Though I'd prefer you to use angular 1.2.27 AngularJS 1.6.X, Which is more stable version & latest release of AngularJS.

工作Plunkr

更新:

您可以通过在 angular.config 中允许全局控制器声明来将当前代码设置为工作状态。但这不是运行角度应用程序的正确方法。

You could do your current code to working state by allow global controller declaration inside angular.config. But this isn't the correct way to run angular application.

function Ctrl($scope) {
    $scope.age = 24;
}

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

这篇关于Angularjs Uncaught Error:[$ injector:modulerr]迁移到V1.3时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆