Javascript 错误:[ngRepeat:iexp]预期表达式的形式为“\u集合中的\u项”\u[track by\u id\u]”,但在控制器中得到了“(姓名)。学生”
您好,我正在尝试使用ng repeat迭代json对象,当我迭代数组时,它工作得很好,但使用json对象时失败了 以下是我的javascript代码:Javascript 错误:[ngRepeat:iexp]预期表达式的形式为“\u集合中的\u项”\u[track by\u id\u]”,但在控制器中得到了“(姓名)。学生”,javascript,angularjs,json,Javascript,Angularjs,Json,您好,我正在尝试使用ng repeat迭代json对象,当我迭代数组时,它工作得很好,但使用json对象时失败了 以下是我的javascript代码: var appModule = angular.module('appModule',[]); appModule.controller('appController',[function(){ this.students = { "abc":"abc", "xyz":"xyz", "x
var appModule = angular.module('appModule',[]);
appModule.controller('appController',[function(){
this.students = {
"abc":"abc",
"xyz":"xyz",
"xyz":"xyz"
}
}]);
这是我的html代码:
<!DOCTYPE html>
<html ng-app="appModule">
<head>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.19/angular.js"></script>
</head>
<body ng-controller="appController as controller">
<div ng-repeat="(firstName,lastName)in controller.students">
<span>Name:{{firstName}} Surname: {{lastName}}</span>
</div>
<script type="text/javascript" src="./app.js"></script>
</body>
</html>
试试这个
<body ng-controller="appController as controller">
<div ng-repeat="(firstName,lastName) in controller.students">
<span>Name:{{firstName}} Surname: {{lastName}}</span>
</div>
您只是缺少一个空格:firstName、lastName和in之间应该有空格
<body ng-controller="appController as controller">
<div ng-repeat="(firstName, lastName) in controller.students">
<span>Name:{{firstName}} Surname: {{lastName}}</span>
</div>
</body>
您可以尝试以这种方式显示数据
<body ng-controller="appController as controller">
<div ng-repeat="student in controller.students">
<span>Name:{{student.firstName}} Surname: {{student.lastName}}</span>
</div>
</body>
这真的是解决办法吗?仍然会出现同样的错误。虽然这段代码可能会解决这个问题,但如何以及为什么解决这个问题将真正有助于提高您的帖子质量,并可能导致更多的投票。请记住,你是在将来回答读者的问题,而不仅仅是现在提问的人。请在回答中添加解释,并说明适用的限制和假设。