【发布时间】:2017-01-21 07:37:36
【问题描述】:
我不熟悉角度。但是我的项目的前端开发人员坚持认为他想要这样的 json:
{
"data": [{
"area1": {
"rows": [{
"the_desc": "A value 1",
"value": "sample value 1"
}, {
"the_desc": "A value 2",
"value": "sample value 2"
}, {
"the_desc": "A value 3",
"value": "other 3"
}, {
"the_desc": "A value 4",
"value": "other 4"
}, {
"the_desc": "A value 5",
"value": "other goats fly"
}, {
"the_desc": "A value 6",
"value": "bla blah"
}, {
"the_desc": "A value 7",
"value": "other 7"
}, {
"the_desc": "A value 8",
"value": "other 8"
}]
}
}, {
"area2": {
"rows": [{
"the_desc": "A value 9",
"value": "sample value 9"
}, {
"the_desc": "A value 10",
"value": "sample value 10"
}, {
"the_desc": "A value 11",
"value": "other 11"
}, {
"the_desc": "A value 12",
"value": "other 12"
}, {
"the_desc": "A value 13",
"value": "other goats fly 13"
}, {
"the_desc": "A value 14",
"value": "bla blah"
}, {
"the_desc": "A value 15",
"value": "other 15"
}, {
"the_desc": "A value 16",
"value": "other 16"
}]
}
}]
}
我认为我们应该去掉像“the_desc”和“value”这样的重复字符串,并使用:
{
"data": [{
"area1": [
["1", "2"],
["3", "4"]
]
}, {
"area2": [
["21", "22", "a5"],
["23", "24", "b6"]
]
}]
}
但他坚持认为 NG-repeat 将无法获取内部数组(它们是已知的列数据行。可能是 [][] 表数据。 问题:在角度 1 中是否存在类似 gettig 数据的问题?没有嵌套的 NG 重复。如果我说每个区域的列都是固定的,会有什么不同吗?有没有办法遍历行,并通过索引访问列?在我们的例子中,每个区域的列数是已知的(页面上的表格)。
原因:来自服务器的负载减少。更快的网络数据传输。
在两个答案的帮助下得到它,在 https://plnkr.co/edit/DrsXTP4kD0CnyAQwuoSu?p=preview 和可爱的角度错误报告上进行试验:https://docs.angularjs.org/error/ngRepeat/dupes?p0=ele%20in%20data.data%5B1%5D.area2%5B0%5D&p1=string:col%203%20A&p2=col%203%20A。
解决方案要点:
在js控制器中:
$scope.data = {
"data": [{"area1":
[[ "A value 1",...
还有 HTML:
<div class= "" ng-repeat="ele in data.data[1].area2 track by $index">
<span class='d2'> {{(ele[0])}} </span><span class='d2'> {{(ele[1])}}...
【问题讨论】:
标签: javascript angularjs json multidimensional-array