Angular中ng-repeat与ulli的多层嵌套重复问题-成都快上网建站

Angular中ng-repeat与ulli的多层嵌套重复问题

学习ng-repeat的过程中常常使用到ng-repeat=”item in array”来循环输出所需要的元素,但是当我们需要使用ng-repeat进行多层嵌套时则容易出现数据重复的问题。

创新互联公司专注于高港企业网站建设,自适应网站建设,商城建设。高港网站建设公司,为高港等地区提供建站服务。全流程按需策划,专业设计,全程项目跟踪,创新互联公司专业和态度为您提供的服务

如在加载与点击事件于js中进行两次http请求获取data分别为list01[]数组和list02[]赋值再如下显示则会导致it1下每个ul都会重复输出list02[]数组中的内容

  • {{it1.value}}
    • {{it2.value}}

输出如:

  1-1
  1-2
  1-1
  1-2

而非

  1-1
  1-2
  2-1
  2-2

其实这个也不是问题,只要换一种思维方式将列表和父级元素相关联形成树状结构就好了

HTML中代码如下

  • {{it1.name}}
    • {{it2.name}}
      • {{it3.name}}
        • {{it4.name}}

js中代码如下

$http.get('ng01.json').success(function (_data) {
  $scope.list01 = [];
  $scope.list01 = _data.data;
});
$scope.show = function (it1, it2, it3) {
  if(it3 != null){
    $http.get("ng04.json").success(function (_data) {
      $scope.list04 = [];
      $scope.list04 = _data.data;
      for(var x in $scope.list03){
        $scope.list03[x].showChild = false;
      }
      it3.showChild = true;
      it3.child = _data.data;
     });
     return;
  }
  if(it2 != null){
    $http.get("ng03.json").success(function (_data) {
      $scope.list03 = [];
      $scope.list03 = _data.data;
      for(var x in $scope.list02){
        $scope.list02[x].showChild = false;
      }
      it2.showChild = true;
      it2.child = _data.data;
    });
    return;
  }
  $http.get("ng02.json").success(function (_data) {
    $scope.list02 = [];
    $scope.list02 = _data.data;
    for(var x in $scope.list01){
      $scope.list01[x].showChild = false;
    }
    it1.showChild = true;
    it1.child = _data.data;
  });
}

json文件都是这样的

{
 "success":1,
 "data":[
  {"name":"01"},
  {"name":"02"}
 ]
}

总结

以上所述是小编给大家介绍的Angular中ng-repeat与ul li的多层嵌套,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对创新互联网站的支持!


新闻标题:Angular中ng-repeat与ulli的多层嵌套重复问题
链接URL:http://kswjz.com/article/gjphjd.html
扫二维码与项目经理沟通

我们在微信上24小时期待你的声音

解答本文疑问/技术咨询/运营咨询/技术建议/互联网交流