你能解释一下为什么下面的代码会返回Error: error:areq Bad Argument吗?
(见live example)
<!DOCTYPE html>
<html ng-app>
<head>
<meta http-equiv="content-type" content="text/html;charset=utf-8" />
<script>
function SimpleController($scope) {
$scope.users = [
{name: 'Dave Jones', city: 'Phoenix'},
{name: 'Jane True', city:'Washington'}
];
}
</script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.4/angular.min.js"></script>
</head>
<body ng-controller="SimpleController">
<ul>
<li ng-repeat="user in users">{{ user.name }}</li>
</ul>
</body>
</html>
答案 0 :(得分:1)
您没有正确定义控制器,请尝试以下操作:
<!DOCTYPE html>
<html ng-app="testApp">
<head>
<meta http-equiv="content-type" content="text/html;charset=utf-8" />
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.4/angular.min.js"></script>
<script>
var testApp = angular.module('testApp', []);
testApp.controller('SimpleCtrl', function ($scope) {
$scope.users = [
{name: 'Dave Jones', city: 'Phoenix'},
{name: 'Jane True', city:'Washington'}
];
})
</script>
</head>
<body ng-controller="SimpleCtrl">
<ul>
<li ng-repeat="user in users">{{ user.name }}</li>
</ul>
</body>
</html>
答案 1 :(得分:1)
在实际应用中,您不应该在全局窗口范围内定义控制器, 在最新版本的angular中,$ controllerProvider在窗口范围内默认不查找, 因为这是一种不好的做法,只能用于演示目的。
但是,您可以通过调用allowGlobals()
:
angular.module('myApp').config(['$controllerProvider', function($controllerProvider) {
// this option might be handy for migrating old apps, but please don't use it
// in new ones!
$controllerProvider.allowGlobals();
}]);
替代和推荐的方法是在模块中定义它。
官方文档可以为您提供很多帮助 https://docs.angularjs.org/guide/controller