嗨,我是棱角分明的新手,只是想学习如何做一些事情。我一直试图显示以下数据。使用chrome的Batarang插件我可以看到我的restful webservice返回包含在我的模型中的以下json。
{
course: {
country: Test1
numberEnrolledPerMonthPerWeek: {
entry:
[ {
key: 2
value: {
numberEnrolled: 0
weeks: 2
year: 2011
}
} , {
key: 3
value: {
numberEnrolled: 4
weeks: 3
year: 2011
}
} , {
key: 4
value: {
numberEnrolled: 6
weeks: 4
year: 2011
}
} , {
key: 8
value: {
numberEnrolled: 0
weeks: 8
year: 2011
}
}
]
}
}
}
我正在尝试将每个键的numberEnrolled值输出到列中。所以在我的HTML中我有以下
<table class="table table-striped table-bordered">
<tr ng-repeat="course in enrolledcourses.enrolledEnrolment">
<td>
{{course.country}}
</td>
<td>
{{course.numberEnrolledPerMonthPerWeek[2].numberEnrolled}}
</td>
</tr>
</table>
{{course.numberEnrolledPerMonthPerWeek [2] .numberEnrolled}}没有给我任何值,所以任何人都可以告诉我们获取numberEnrolled值的正确语法是什么。
我试过了
{{course.numberEnrolledPerMonthPerWeek.2.numberEnrolled}}
{{course.numberEnrolledPerMonthPerWeek[2][numberEnrolled]}}
我的控制器代码如下
.controller('PeopleCtrl', function($scope, recruitmentFactory) {
$scope.enrolledcourses = recruitmentFactory.get();
$scope.test = "hello";
$scope.save = function() {
alert("save has been called");
};
})
答案 0 :(得分:2)
首先,你的JSON有很多错误。也许这只是你得到的输出,但以防万一:: 缺少逗号而Test是一个符号,但我猜它是一个字符串。
$scope.enrolledcourses = {
course : {
country: 'Test1',
numberEnrolledPerMonthPerWeek: {
entry: [{
key: 2,
value: {
numberEnrolled: 0,
weeks: 2,
year: 2011,
}
}, {
key: 3,
value: {
numberEnrolled: 4,
weeks: 3,
year: 2011
}
}, {
key: 4,
value: {
numberEnrolled: 6,
weeks: 4,
year: 2011
}
}, {
key: 8,
value: {
numberEnrolled: 0,
weeks: 8,
year: 2011
}
}]
}
}
};
然后你的html需要访问正确的属性:
<tr ng-repeat="course in enrolledcourses">
<td>{{course.country}}</td>
<td>{{course.numberEnrolledPerMonthPerWeek.entry[2].value.numberEnrolled}}</td>
</tr>
答案 1 :(得分:1)
只是为了提供一些额外的帮助:我通过弄清楚如何浏览JSON数据来解决这个问题。尝试使用可视化工具,并验证您的JSON以确保其正确。
以下是我用来可视化数据的内容: http://jsonviewer.stack.hu/ 验证在这里: http://jsonlint.com/
答案 2 :(得分:0)
numberEnrolledPerMonthPerWeek[2] is not your array
尝试
numberEnrolledPerMonthPerWeek.entry[2]
答案 3 :(得分:0)
好的,所以我做了以下工作以获得价值
{{course.numberEnrolledPerMonthPerWeek.entry[2].value.numberEnrolled}}