Javascript Angularjs 未捕获错误:[$injector:modulerr] 迁移到 V1.3 时
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28727919/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Angularjs Uncaught Error: [$injector:modulerr] when migrating to V1.3
提问by Nakib
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.
我正在学习 Angular.js,但我无法弄清楚这个简单的代码有什么问题。它看起来不错,但给了我以下错误。
**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)
And before adding ng-app="app"(I was just keeping it as ng-app) it was giving me following errors. Why is that?
在添加之前(我只是将其保留为),它给了我以下错误。这是为什么?ng-app="app"ng-app
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>
回答by Pankaj Parkar
After AngularJS version 1.3 global controller function declaration is disabled
AngularJS 1.3 版全局控制器函数声明被禁用后
You need to first create an AngularJS module & then attach all the components to that specific module.
您需要首先创建一个 AngularJS 模块,然后将所有组件附加到该特定模块。
CODE
代码
function Ctrl($scope) {
$scope.age = 24;
}
angular.module('app', [])
.controller('Ctrl', ['$scope', Ctrl]);
Specifically for your case, there is some issue with AngularJS 1.3.14(downgrade it to 1.3.13works fine). Though I'd prefer you to use angular 1.2.27AngularJS 1.6.X, Which is more stable version & latest release of AngularJS.
特别是对于您的情况,AngularJS 存在一些问题1.3.14(将其降级以1.3.13正常工作)。虽然我更喜欢你使用angular 1.2.27AngularJS 1.6.X,这是更稳定的版本和最新版本的 AngularJS。
UPDATE:
更新:
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.
您可以通过允许在内部进行全局控制器声明来使当前代码处于工作状态angular.config。但这不是运行 angular 应用程序的正确方法。
function Ctrl($scope) {
$scope.age = 24;
}
angular.module('app', [])
.config(['$controllerProvider',
function ($controllerProvider) {
$controllerProvider.allowGlobals();
}
]);
回答by Harkirat Saluja
I was myself stuck in this issue for sometime. Check for following in order:-
我自己被这个问题困住了一段时间。按顺序检查以下内容:-
The path to you angular.js script is correct (whether you are calling it in your HTML from a local source or as an external resource).
Next, once your angular.js is correct check if your app is initialized or not.
var app=angular.module('app',[])//in your app.js file<body ng-app="app">//in your htmlNext register your controller with the app and pass in all necessary injections
app.controller('myCtrl',function(){});Call your javascript file in your html file
<script src="app.js"></script>
angular.js 脚本的路径是正确的(无论您是在 HTML 中从本地源还是作为外部资源调用它)。
接下来,一旦您的 angular.js 正确,请检查您的应用程序是否已初始化。
var app=angular.module('app',[])//在你的 app.js 文件中<body ng-app="app">//在你的html中接下来在应用程序中注册您的控制器并传递所有必要的注入
app.controller('myCtrl',function(){});在 html 文件中调用 javascript 文件
<script src="app.js"></script>
回答by Arielle Nguyen
You have to define your controller
你必须定义你的控制器
var app = angular.module('app', []);
app.controller('Ctrl', ['$scope',function($scope) {
$scope.age = 24;
}]);
回答by susan097
Be Sure that ng-app="app_name"define must match to
确保ng-app="app_name"定义必须匹配
var app=angular.module('app_name',[])
var app=angular.module('app_name',[])
<html ng-app="myApp">
<body>
<div ng-controller="Ctrl">
<input ng-model="name">
<h1>{{name}}</h1>
<h2>{{age}}</h2>
</div>
<script>
var app = angular.module('myApp',[]); // same to the above define appName
app.controller('Ctrl',function($scope){
$scope.age=24; // initialize age by injecting scope
});
</script>
</body>
<html>
For more details visit Here
欲了解更多详情,请访问这里

