[英]Angular js- Error: ng:areq Bad Argument-
I saw the there are several question regarding this error, but I didn't find the answer for my case. 我看到有关此错误的几个问题,但我没有找到适合我的情况的答案。
I am new to angular and started to build small app. 我是新手,因此开始构建小型应用程序。
<!doctype html>
<html lang="en" ng-app>
<head>
<meta charset="UTF-8">
<title>Angular Demo</title>
<script src="lib/angular/angular.min.js"></script>
</head>
<body>
<div ng-controller =" MyController">
<h1>{{player.name}}</h1>
</div>
<script>
function MyController($scope) {
$scope.player = {
'name': 'Eran Zahavi',
'number': '7',
'position': 'link'
}
}
</script>
</body>
</html>
When I tried to run it I got the error above--> Error: [ng:areq] http://errors.angularjs.org/1.4.3/ng/areq?p0=MyController&p1=not%20a%20function%2C%20got%20undefined 当我尝试运行它时,出现上面的错误->错误:[ng:areq] http://errors.angularjs.org/1.4.3/ng/areq?p0=MyController&p1=not%20a%20function%2C %20got%20undefined
If you are refering to angular above 1.3 version, you should declare controller different way, 如果您是指1.3以上版本的Angle,则应以其他方式声明控制器,
var newApp = angular.module('newApp', []);
newApp.controller('MyController', function($scope){
$scope.player = {
'name': 'Eran Zahavi',
'number': '7',
'position': 'link'
}
});
From angular 1.4.x you can't define controller globally. 从angular 1.4.x开始,您无法全局定义控制器。
You have to declare controller inside module. 您必须在模块内部声明控制器。
Like this 像这样
<!doctype html>
<html lang="en" ng-app="app">
<head>
<meta charset="UTF-8">
<title>Angular Demo</title>
<script src="lib/angular/angular.min.js"></script>
</head>
<body>
<div ng-controller=" MyController">
<h1>{{player.name}}</h1>
</div>
<script>
var app = angular.module("app", []);
app.controller("MyController", function($scope) {
$scope.player = {
'name': 'Eran Zahavi',
'number': '7',
'position': 'link'
}
});
</script>
</body>
</html>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.