角度 1:使用服务器 JSON 中的二维数组用 ng-repeat 填充表



我不熟悉angular。但是为我的项目工作的前端开发人员坚持他想要这样的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"one_answers"value",并使用:

{
"data": [{
    "area1": [
        ["1", "2"],
        ["3", "4"]
    ]
}, {
    "area2": [
        ["21", "22", "a5"],
        ["23", "24", "b6"]
    ]
}]
}

但是他坚持认为NG-repeat将无法获得内部数组(它们是已知的数据列行)。可以是[][]表数据。问题:在角1中获取这样的数据有问题吗?没有嵌套的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])}}...

作为前端开发人员,我也会选择第一种结构。在任何情况下,您都必须像这样使用嵌套的ng-repeat:

<div class="area" ng-repeat="area in data">
    <div class="row" ng-repeat="row in area.rows">
        <p class="description">{{::row.the_desc}}</p>
        <p class="value">{{::row.value}}</p>
    </div>
</div>

第二个代码片段:

<div class="area" ng-repeat="area in data">
    <div class="row" ng-repeat="(key, value) in area.rows">
        <p class="description">{{::key}}</p>
        <p class="value">{{::value}}</p>
    </div>
</div>

对于第三段代码,假设数组结构保持不变并且只有两个值,我们将能够通过索引访问它们。

<div class="area" ng-repeat="(key,area) in data">
    <!-- Prints area1, area2 if necessary -->
    <p>{{key}}</p>
    <!-- Prints 1st and 2nd value of the area array -->  
    <div class="row" ng-repeat="row in area">               
        <p class="description">{{row[0}}</p>
        <p class="value">{{row[1]}}</p>
    </div>
</div>

是否有可能在没有嵌套的ng-repeat的情况下实现这一点?不太可能。嵌套数组太多

但老实说,我不认为在object key中存储"value"是一个好的做法(即使它可以用Angular显示),所以我建议你遵循开发者的建议,使用第一个结构。

不,根本不需要,ng-repeats可以嵌套。即使在普通的Javascript中,您也可以将这些数据转换成您想要的任何形式,以便使ng-repeat按所需显示它。最后两个选项实际上会让这变得更困难。

对于第一个配置,假设它是JSON.parse() ed:

<section ng-repeat="area in data">
  <div ng-repeat="row in area.rows">
    <p>{{row.the_desc}} is {{row.value}}</p>
  </div>
</section>

这遍历data数组,并且对于data中的每个area,为area#.rows数组创建另一个ng-repeat以遍历行。

甚至可以使用ng-repeat来迭代对象中的键,这需要使用其他两种解决方案来完成。

我认为您需要稍微改变一下您的数据结构。像这样设置应该会有帮助:

$scope.data = [{
    name: "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"
          }]
}, 
{
   name: "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"
    }]
}

那么使用ng-repeat就很容易访问了,像这样:

      <div ng-repeat="d in data">
    <h1>{{d.name}}</h1>
    <div ng-repeat="r in d.rows">
      {{r.the_desc}}
    </div>
  </div>

这是一个普朗克。我不确定您是如何获得数据的,或者它是否是硬编码的,但这至少应该让您开始。

相关内容

  • 没有找到相关文章

最新更新