【发布时间】:2015-11-02 12:46:39
【问题描述】:
无法弄清楚我在哪里犯了错误。 在没有任何运气的情况下尝试了此操作- How to find a reason AngularJS "Argument 'MyCtrl' is not a function, got undefined"
这是我的 index.html 文件 -
<!DOCTYPE>
<html ng-app="myApp">
<head><title>Angular JS table sort and filter example </title>
<link rel="stylesheet" href="//netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap.min.css">
<script src="http://code.angularjs.org/1.3.12/angular.js"></script>
<script src="http://code.angularjs.org/1.3.12/angular-resource.js"></script>
<script src="http://angular-ui.github.io/bootstrap/ui-bootstrap-tpls-0.11.0.js"></script>
<style>
.odd {
background-color: antiquewhite;
color: #008b8b;
}
td th {
height: 30px;
min-width: 100px;
}
thead {
background-color: darkgray;
color: white;
height: 30px;
}
</style>
</head>
</head>
<body ng-app="myApp">
<div ng-controller="myCtrl">
<h3>List students</h3>
<div class="container-fluid">
<pre>Click header link to sort, input into filter text to filter</pre>
<hr />
<table class="table table-striped">
<thead>
<tr>
<th>
<a href="" ng-click="order('name')">Name</a>
</th>
<th><a href="" ng-click="order('age')"> Age</a> </th>
<th><a href="" ng-click="order('gender')">Gender</a> </th>
</tr>
</thead>
<tbody>
<tr>
<td> <input type="text" ng-model="search.name" /></td>
<td> <input type="text" ng-model="search.age" /> </td>
<td><input type="text" ng-model="search.gender" /> </td>
</tr>
<tr ng-repeat="user in students | orderBy:predicate:reverse | filter:paginate| filter:search" ng-class-odd="'odd'">
<td>{{user.name}}</td>
<td>{{user.age}}</td>
<td>{{user.gender}}</td>
</tr>
</tbody>
</table>
<pagination total-items="totalItems" ng-model="currentPage"
max-size="5" boundary-links="true"
items-per-page="numPerPage" class="pagination-sm">
</pagination>
</div>
</div>
<script src="controllers/controller.js"></script>
</body>
</html>
controller.js 文件 -
var app = angular.module('myApp',['ui.bootstrap', 'ngResource']);
app.controller('myCtrl', function ($scope) {
$scope.predicate = 'name';
$scope.reverse = true;
$scope.currentPage = 1;
$scope.order = function (predicate) {
$scope.reverse = ($scope.predicate === predicate) ? !$scope.reverse : false;
$scope.predicate = predicate;
};
$scope.students = [
{ name: 'Kevin', age: 25, gender: 'boy' },
{ name: 'John', age: 30, gender: 'girl' },
{ name: 'Laura', age: 28, gender: 'girl' },
{ name: 'Joy', age: 15, gender: 'girl' },
{ name: 'Mary', age: 28, gender: 'girl' },
{ name: 'Peter', age: 95, gender: 'boy' },
{ name: 'Bob', age: 50, gender: 'boy' },
{ name: 'Erika', age: 27, gender: 'girl' },
{ name: 'Patrick', age: 40, gender: 'boy' },
{ name: 'Tery', age: 60, gender: 'girl' }
];
$scope.totalItems = $scope.students.length;
$scope.numPerPage = 5;
$scope.paginate = function (value) {
var begin, end, index;
begin = ($scope.currentPage - 1) * $scope.numPerPage;
end = begin + $scope.numPerPage;
index = $scope.students.indexOf(value);
return (begin <= index && index < end);
};
});
【问题讨论】:
-
控制器构造函数必须传入数组
app.controller('myCtrl', ['$scope', function($scope) { //your controller code here}]); -
不起作用 - 这是 controller.js 现在的样子。我犯错了吗? var app = angular.module('myApp',['ui.bootstrap', 'ngResource']); app.controller('myCtrl', ['$scope', function($scope) { //mycontroller 代码在这里 }]);
-
奇怪的是,代码似乎在本地运行良好。我注意到您在 html 中声明了 2 个
ng-apps。你可能只需要一个。 -
我同意@danielepolencic - 尝试从
body标签中删除第二个ng-app属性,只留下html标签 -
它起作用了 - @danielepolencic - 来自 Andrew 的提示加上您解决了错误。昨晚我睡不着,因为它在我的脑海里。谢谢!
标签: javascript jquery angularjs mean-stack