[英]Why is Angular not seeing my controller?
I am busy following a tutorial, as a total Angular novice, and have reached the point where the Controller is introduced. 作为Angular的新手,我正忙于按照一个教程进行操作,并且已经达到了介绍Controller的地步。 My MVC view is generating the following markup:
我的MVC视图正在生成以下标记:
<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>
When I used the ng-init
directive to instantiate the person array 'inline', eg: 当我使用
ng-init
指令实例化人员数组“ inline”时,例如:
<table class="table" data-ng-init="persons=[....]">
the data binding in the table worked, but when I use the ng-controller
directive, as in my example above, I get an error in Chrome's console stating: 表中的数据绑定有效,但是当我使用
ng-controller
指令时(如上面的示例),我在Chrome的控制台中收到一条错误消息,指出:
Error: [ng:areq] Argument 'PersonController' is not a function, got undefined
错误:[ng:areq]参数'PersonController'不是一个函数,未定义
It looks like you need to define your controller in a different fashion. 看起来您需要以其他方式定义控制器。 I don't see an
app
definition in your example so be sure you have something that looks like the following, then chain your controller off app
在您的示例中没有看到
app
定义,因此请确保您具有类似以下内容的内容,然后将控制器与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' }
];
}]);
You need to define your angular js module and inject $scope in PersonController. 您需要定义您的angular js模块并将$ scope注入PersonController中。
<script>
angular.module('app', []);
function PersonController($scope) {
$scope.persons = [
{ firstName: 'Brady', lastName: 'Kelly' },
{ firstName: 'James', lastName: 'Brown' },
{ firstName: 'Charles', lastName: 'Manson' }
];
}
PersonController.$inject = ['$scope'];
</script>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.