简体   繁体   中英

Multiple Nested ng-repeat-start fails to display data

I am trying to flatten out a nested json object & display it in a table.

So I tried using multiple ng-repeat-start & ng-repeat-end elements in a div. But somehow data is not getting displayed.

Plunkr : http://plnkr.co/edit/apuf9YUYnkoSaCDaNYrC?p=preview

HTML :

<!DOCTYPE html>
<html ng-app="plunker">

  <head>
    <meta charset="utf-8" />
    <title>AngularJS Plunker</title>
    <script>document.write('<base href="' + document.location + '" />');</script>
    <link rel="stylesheet" href="style.css" />
    <script data-require="angular.js@1.2.x" src="https://code.angularjs.org/1.2.22/angular.js" data-semver="1.2.22"></script>
    <script src="app.js"></script>
  </head>

  <body ng-controller="MainCtrl">

    Data is  : 
    {{mydata}}

    <table cellspacing="1" border="1">
      <thead>
        <tr> 
        <td> MainTitle</td>
        <td> SubTitle</td>
        <td> LastTitle</td>
        <td> Final</td>
        </tr>

      </thead>
      <tbody>
      <div ng-repeat-start="main in mydata" ng-if="false"></div>    
      <div ng-repeat-start="sub in main.items" ng-if="false"></div>    
      <div ng-repeat-start="last in sub.l2items" ng-if="false"></div>


      <tr ng-repeat="final in last.l3items">
        <td> {{main.title}} </td>
        <td> {{sub.title}} </td>
        <td> {{last.title}} </td>
        <td> {{final}} </td>

      </tr>
      </tbody>
      <div ng-repeat-end ng-if="false"></div>    
      <div ng-repeat-end ng-if="false"></div>    
      <div ng-repeat-end ng-if="false"></div>

    </table>
  </body>

</html>

Code:

var app = angular.module('plunker', []);

app.controller('MainCtrl', function($scope) {
  $scope.name = 'World';



    $scope.mydata = [
      {
        title :"hello",
        items : [
          {
          title: "level 1 title",
          l2items : [
            {
            title : "level 2 title",
            l3items: [ "a","b" ]

           }
            ]
        }
        ,
         {
          title: "level 1 title 1 ",
          l2items : [
            {
            title : "level 2 title 1",
            l3items: [ "a 1","b 1" ]
           }
            ]
        }
      ]
      },{
      title :"hello 0",
        items : [
          {
          title: "level 1 title 0",
          l2items : [
            {
            title : "level 2 title 0",
            l3items: [ "a 0","b 0" ]

           }
            ]
        }
        ,
         {
          title: "level 1 title 1 0",
          l2items : [
            {
            title : "level 2 title 1 0",
            l3items: [ "a 1 0","b 1 0" ]
           }
            ]
        }
      ],
      }]
});

Expected Output : (apologies could not create a proper table)

Main title | Subtitle | LastTitle | Final

hello |level 1 title |level 2 title |a

hello |level 1 title |level 2 title |b

hello |level 1 title 1 |level 2 title 1 |a 1

hello |level 1 title 1 |level 2 title 1 |b 1

hello 0 |level 1 title 0 |level 2 title 0 |a 0

hello 0 |level 1 title 0 |level 2 title 0 |b 0

hello 0 |level 1 title 1 0 |level 2 title 1 0 |a 1 0

hello 0 |level 1 title 1 0 |level 2 title 1 0 b 1 0

I believe the problem is with your HTML. Most likely having divs inside your table. If you convert it to <div> s then it works. Another issue I saw was that you were repeating your closing </body> tag.

I understand why you were doing it with the <div> s and ng-if s. There was, at one time, a comment directive version of the ng-repeat , or at least an effort to create one, but I think they ran into browser limitations .

Here is a similar issue . Perhaps you'll like their solution.

Otherwise, replace your <div> s with <tr> s:

  <tr ng-repeat-start="main in mydata" ng-if="false"></tr>
  <tr ng-repeat-start="sub in main.items" ng-if="false"></tr>
  <tr ng-repeat-start="last in sub.l2items" ng-if="false"></tr>

  <tr ng-repeat="final in last.l3items">
    <td> {{main.title}} </td>
    <td> {{sub.title}} </td>
    <td> {{last.title}} </td>
    <td> {{final}} </td>

  </tr>

  <tr ng-repeat-end ng-if="false"></tr>
  <tr ng-repeat-end ng-if="false"></tr>
  <tr ng-repeat-end ng-if="false"></tr>

http://plnkr.co/edit/jPtHDbaonUtc9tXQadc2?p=preview

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM