作为一名 Angular 新手,我正在忙着学习教程,并且已经到了引入 Controller 的阶段。我的 MVC View 生成以下标记:
<script>
function PersonController($scope) {
$scope.persons = [
{ firstName: 'Brady', lastName: 'Kelly' },
{ firstName: 'James', lastName: 'Brown' },
{ firstName: 'Charles', lastName: 'Manson' }
];
}
</script>
<table class="table" data-ng-controller="PersonController">
<tr>
<th>
<span>Last Name</span>
</th>
<th>
<span>First Name</span>
</th>
<th></th>
</tr>
<tr data-ng-repeat="pers in persons | orderBy:'lastName'">
<td>
{{pers.lastName}}
</td>
<td>
{{pers.firstName}}
</td>
<td></td>
</tr>
</table>
当我使用 ng-init
指令实例化 person 数组“内联”时,例如:
<table class="table" data-ng-init="persons=[....]">
表中的数据绑定(bind)有效,但是当我使用 ng-controller
指令时(如上面的示例所示),我在 Chrome 控制台中收到错误消息,指出:
Error: [ng:areq] Argument 'PersonController' is not a function, got undefined
最佳答案
看起来您需要以不同的方式定义 Controller 。我在您的示例中没有看到 app
定义,因此请确保您有如下所示的内容,然后将您的 Controller 链接到 app
<html ng-app="app">
<小时/>
var app = angular.module('app',
[
]);
app.controller('PersonController', ['$scope', function ($scope) {
$scope.persons = [
{ firstName: 'Brady', lastName: 'Kelly' },
{ firstName: 'James', lastName: 'Brown' },
{ firstName: 'Charles', lastName: 'Manson' }
];
}]);
关于javascript - 为什么 Angular 看不到我的 Controller ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29698588/