我只是想让我的头了解AngularJS的基础知识。我试图编写一个简单的MVC应用程序,但控制器似乎没有工作。该文件可以找到刚才找到的角度库,我已经通过排除'ng-controller'来测试。AngularJS初学者:ng控制器不工作
<!DOCTYPE html>
<html ng-app>
<head>
<script src='js/lib/angular.js'></script>
<script>
// controller
function MyFirstCtrl($scope) {
// model
var employees = ['Christopher Grant', 'Monica Grant', 'Christopher
Grant', 'Jennifer Grant'];
$scope.ourEmployees = employees;
}
</script>
</head>
<body ng-controller='MyFirstCtrl'>
<!-- view -->
<h2>Number of employees: {{ourEmployees.length}}</h2>
</body>
</html>
编辑: 错误日志说以下内容:
Uncaught SyntaxError: Unexpected token ILLEGAL , line 9
Uncaught SyntaxError: Unexpected token { , line 19
Error: [ng:areq] Argument 'MyFirstCtrl' is not a function, got undefined
EDIT2:我改变了代码如下:
<!DOCTYPE html>
<html ng-app='MVCExample'>
<head>
<script src='js/lib/angular.js'></script>
<script>
var app = angular.module('MVCExample', []);
// controller
app.controller("MyFirstCtrl", function($scope) {
// model
var employees = ['Christopher Grant', 'Monica Grant', 'Christopher Grant', 'Jennifer Grant'];
$scope.ourEmployees = employees;
});
</script>
</head>
<body ng-controller='MyFirstCtrl'>
<!-- view -->
<h2>Number of employees: {{ourEmployees.length}}</h2>
</body>
</html>
它还原来, '雇员' 阵列因为我有一个字符串入口分成两行,所以我是非法的。以上作品。我使用的初学者书籍必须过时,这是不幸的。
你能保证吗? – Vineet
错误控制台说什么?确保'Christopher'和'Grant'之间没有实际的换行符。 – Ronnie
我正在使用1.4.2。 – BrianRT