我有以下 Controller :
<div ng-app="" ng-controller="studentController">
<table>
<tr>
<th>Name</th>
<th>Marks</th>
</tr>
<tr ng-repeat="subject in student.subjects">
<td> {{ subject.name }} </td>
<td> {{ subject.marks }} </td>
</tr>
</table>
</div>
我有以下脚本:
function studentController($scope) {
$scope.student = {
subjects:[
{name:'Physics',marks:70},
{name:'Chemistry',marks:80},
{name:'Math',marks:65},
{name:'English',marks:75},
{name:'Hindi',marks:67}
]
};
}
在控制台中,错误表明 studentController
不是函数。所以它可能来自 JS,但我没有注意到任何想法?
最佳答案
Global controllers are no longer supported ;您必须正确定义它们。请参阅 SO 问题:Why doesn't the latest version of angular support global controller functions?有关这方面的一些细节 - 因为您仍然会发现使用这种旧模式的孤立教程。观察以下...
angular.module('app', [])
.controller('studentController', function($scope) {
/*...*/
});
<div ng-app="app" ng-controller="studentController">
<table>
<tr>
<th>Name</th>
<th>Marks</th>
</tr>
<tr ng-repeat="subject in student.subjects">
<td> {{ subject.name }} </td>
<td> {{ subject.marks }} </td>
</tr>
</table>
</div>
JSFiddle Link - 工作演示
关于javascript - 为什么我的 AngularJS Controller 定义和标记没有按预期工作? - <controller> 不是函数错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34536172/