我正在尝试学习AngularJs中的控制器和范围,但是当我运行此代码时,我总是得到这个Error: [ng:areq] Argument 'studentController' is not a function, got undefined
。
<html >
<head>
<title>AngularJs</title>
<script src="script/angular.js"></script>
</head>
<body>
<div ng-app="" ng-controller="studentController">
<div class="container" >
<input type="text" ng-model="name">
<br>
<h2>List name using ng-init and ng-repeat</h2>
<ul>
<li ng-repeat="cust in customers | filter : name | orderBy:'city' ">
{{ cust.name }} - {{ cust.city }}
</li>
</ul>
</div>
</div>
<script>
function studentController($scope) {
$scope.customers = [
{name : 'John Smit', city : 'Ifrane'},
{name : 'Nurcan Turkey', city : 'Izmir'},
{name : 'Laura Keller', city : 'Cologne'},
{name : 'Sam Haimoura', city : 'Harhoura'},
{name : 'Brayan Amid', city : 'Casablanca'}
];
};
</script>
</body>
即使这段代码是以某种方式直接来自教程。有人可以告诉我这段代码有什么问题
答案 0 :(得分:4)
ng-app不应为空
<div ng-app="appname" ng-controller="StudentController">
var app = angular.module('appname', []);
app.controller('StudentController', ...
答案 1 :(得分:2)
<div ng-app="appname" ng-controller="StudentController">
var app = angular.module('appname', []);
app.controller('StudentController','');
答案 2 :(得分:1)
<html>
<head>
<title>AngularJs</title>
<script src="script/angular.js"></script>
</head>
<body>
<div ng-app="myApp" ng-controller="studentController">
<div class="container" >
<input type="text" ng-model="name">
<br>
<h2>List name using ng-init and ng-repeat</h2>
<ul>
<li ng-repeat="cust in customers | filter : name | orderBy:'city' ">
{{ cust.name }} - {{ cust.city }}
</li>
</ul>
</div>
</div>
<script>
var myApp = angular.module('myApp', []);
myApp.controller('studentController', ['$scope', function($scope) {
$scope.customers = [
{name : 'John Smit', city : 'Ifrane'},
{name : 'Nurcan Turkey', city : 'Izmir'},
{name : 'Laura Keller', city : 'Cologne'},
{name : 'Sam Haimoura', city : 'Harhoura'},
{name : 'Brayan Amid', city : 'Casablanca'}
];
}]);
</script>
</body>
答案 3 :(得分:1)
答案 4 :(得分:0)
上面的代码似乎在JSFiddle中运行得很好:http://jsfiddle.net/nde4m2ff/。
我认为问题可能是您在页面中过早地初始化angularjs。在定义function studentController() { }
之后,尝试将角度脚本定义移动到最后。
答案 5 :(得分:0)
尝试定义应用并使用其他符号,ng-app不应该为空,使用数组notation建议角色小组如下:
<html >
<head>
<title>AngularJs</title>
<script src="script/angular.js"></script>
</head>
<body>
<div ng-app="myapp" ng-controller="studentController">
<div class="container" >
<input type="text" ng-model="name">
<br>
<h2>List name using ng-init and ng-repeat</h2>
<ul>
<li ng-repeat="cust in customers | filter : name | orderBy:'city' ">
{{ cust.name }} - {{ cust.city }}
</li>
</ul>
</div>
</div>
<script>
angular.module('myapp').controller('studentController', ['$scope',
function($scope) {
$scope.customers = [
{name : 'John Smit', city : 'Ifrane'},
{name : 'Nurcan Turkey', city : 'Izmir'},
{name : 'Laura Keller', city : 'Cologne'},
{name : 'Sam Haimoura', city : 'Harhoura'},
{name : 'Brayan Amid', city : 'Casablanca'}
];
}
]);
</script>
</body>