2017-03-28 16 views
1

私はAngularJsを初めて使用しており、ネストされた配列を持つJsonデータに問題が発生しています。AngularJS ng-repeat in table

私はシンプルなhtmlドキュメントにコードを単純化しましたが、これは以下にあります。

最初のプロパティ{{HelloMessage}}が動作していて、プロパティHelloMessageに格納されている文字列値が入力されますが、ng-repeatはそうではありません。

オンラインで見ると、実際に配列内に配列があることがわかりました。したがって、ng-repeat内にng-repeatが必要であると仮定していましたが、動作しません。私はそれが私が間違っていた単純な何かであると確信しています。

<!DOCTYPE html> 
<html ng-app="app"> 
<head> 
    <title></title> 
</head> 
<body> 
    <h1 ng-controller="myController">{{helloMessage}}</h1> 
<div> 
    <table> 
     <thead> 
      <tr> 
       <th>Id</th> 
       <th>UserId</th> 
       <th>DisplayName</th> 
      </tr> 
     </thead> 
     <tbody> 
      <tr ng-repeat="stream in Data.Records"> 
       <tr ng-repeat="record in stream.Users"> 
        <td>{{stream.Id}}</td> 
        <td>{{stream.User}}</td> 
        <td> 
         {{stream.Description}} 
        </td> 
        <!--<td> 
         <table> 
          <tbody> 
           <tr ng-repeat="b in value"> 
            <td>{{b.user}}</td> 
           </tr> 
          </tbody> 
         </table> 
        </td>--> 
       </tr> 
      </tr> 
     </tbody> 
    </table> 

</div> 

<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.7/angular.min.js"></script> 

<script type="text/javascript"> 
    angular.module('app', []).controller('myController', 
         function ($scope) { 


          $scope.helloMessage = "Hi"; 


          $scope.Data = [{ 
           Success: true, 
           ErrorMessage: null, 
           SuccessMessage: null, 
           Records: [{ 
            "CreatedBy": "Mickey Mouse", 
            "CreatedDate": "2015-08-17T13:16:22.713", 
            "CreatedDateDisplay": "17-08-2015", 
            "Description": "Test 1", 
            "Id": 7546798576985769857, 
            "Name": "Test 1", 
            "UpdatedBy": "", 
            "UpdatedDate": null, 
            "UpdatedDateDisplay": null, 
            "User": null, 
            "UserId": 0, 
            "Users": [{ 
             "Users": [{ 
              "Id": 7546798576985769858, 
              "UserId": 7546798576985769857, 
              "DisplayName": "Daffy Duck" 
             }, { 
              "Id": 7546798576985769859, 
              "UserId": 7546798576985769857, 
              "DisplayName": "Pluto" 
             } 
             ], 
             "User": "Bugs Bunny", 
             "UserId": 7546798576985769857, 
             "Name": "Test 2", 
             "Description": "Test 2", 
             "Id": 7546798576985769857, 
             "CreatedBy": "Goofy", 
             "CreatedDate": "2015-08-25T14:03:28.083", 
             "UpdatedBy": "Porky Pig", 
             "UpdatedDate": "2017-03-27T08:19:36.077", 
             "CreatedDateDisplay": "25-08-2015", 
             "UpdatedDateDisplay": "27-03-2017" 
            } 
            ] 
           } 
           ] 
          } 
          ]; 



         }); 
</script> 
</body> 
</html> 

エラーがクロームコンソール

答えて

1

<tr ng-repeat="stream in Data[0].Records[0].Users[0].Users"> 
</tr> 

として使用

<td>{{stream.Id}}</td> 
    <td>{{stream.UserId}}</td> 
    <td>{{stream.DisplayName}}</td> 

代わりの

をngのリピートを書き直し

:あなたは巣ng-repeatする必要はありません

function myController($scope) { 
 
    $scope.helloMessage = "Hi"; 
 
    $scope.Data = [{ 
 
    Success: true, 
 
    ErrorMessage: null, 
 
    SuccessMessage: null, 
 
    Records: [{ 
 
     "CreatedBy": "Mickey Mouse", 
 
     "CreatedDate": "2015-08-17T13:16:22.713", 
 
     "CreatedDateDisplay": "17-08-2015", 
 
     "Description": "Test 1", 
 
     "Id": 7546798576985769857, 
 
     "Name": "Test 1", 
 
     "UpdatedBy": "", 
 
     "UpdatedDate": null, 
 
     "UpdatedDateDisplay": null, 
 
     "User": null, 
 
     "UserId": 0, 
 
     "Users": [{ 
 
     "Users": [{ 
 
      "Id": 7546798576985769858, 
 
      "UserId": 7546798576985769857, 
 
      "DisplayName": "Daffy Duck" 
 
     }, { 
 
      "Id": 7546798576985769859, 
 
      "UserId": 7546798576985769857, 
 
      "DisplayName": "Pluto" 
 
     }], 
 
     "User": "Bugs Bunny", 
 
     "UserId": 7546798576985769857, 
 
     "Name": "Test 2", 
 
     "Description": "Test 2", 
 
     "Id": 7546798576985769857, 
 
     "CreatedBy": "Goofy", 
 
     "CreatedDate": "2015-08-25T14:03:28.083", 
 
     "UpdatedBy": "Porky Pig", 
 
     "UpdatedDate": "2017-03-27T08:19:36.077", 
 
     "CreatedDateDisplay": "25-08-2015", 
 
     "UpdatedDateDisplay": "27-03-2017" 
 
     }] 
 
    }] 
 
    }]; 
 
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.0.2/angular.min.js"></script> 
 
<div ng-app> 
 
    <div ng-controller="myController"> 
 
    <h1>{{helloMessage}}</h1> 
 
    <table> 
 
     <thead> 
 
     <tr> 
 
      <th>Id</th> 
 
      <th>UserId</th> 
 
      <th>DisplayName</th> 
 
     </tr> 
 
     </thead> 
 
     <tbody> 
 
     <tr ng-repeat="stream in Data[0].Records[0].Users[0].Users"> 
 
      <td>{{stream.Id}}</td> 
 
      <td>{{stream.UserId}}</td> 
 
      <td> 
 
      {{stream.DisplayName}} 
 
      </td> 
 
     </tr> 
 
     </tbody> 
 
    </table> 
 
    </div> 
 
</div>

0

に投げているあなたのJSONは、配列の完全であると思われます。それはおそらく改善されるかもしれませんが、現在の状態でそれを表示する方法があります。

<tbody> 
    <tr ng-repeat="user in Data[0].Records[0].Users[0].Users"> 
     <td>{{user.Id}}</td> 
     ... 
</tbody> 

Working JSFiddle of your code

1

まず次のように身体のレベルにあなたのコントローラを定義する必要があります。

<body ng-controller="myController"> 

h1レベルとして定義されているため、アクセスできません。

私は以下のようにコードを変更しました。

<!DOCTYPE html> 
<html ng-app="app"> 
<head> 
    <title></title 

</head> 
<body ng-controller="myController"> 
    <h1>{{helloMessage}}</h1> 
    <div> 
     <table> 
      <thead> 
       <tr> 
        <th>Id</th> 
        <th>UserId</th> 
        <th>DisplayName</th> 
       </tr> 
      </thead> 
      <tbody> 
       <tr ng-repeat="stream in Data[0].Records[0].Users[0].Users"> 
        <td>{{stream.Id}}</td> 
        <td>{{stream.UserId}}</td> 
        <td>{{stream.DisplayName}} 
        </td> 
       </tr> 
      </tbody> 
     </table> 
    </div> 

</body> 
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.7/angular.min.js"></script>  

    <script type="text/javascript"> 
angular.module('app', []).controller('myController', 
          function ($scope) { 
           $scope.helloMessage = "Hi"; 
           $scope.Data = [{ 
            Success: true, 
            ErrorMessage: null, 
            SuccessMessage: null, 
            Records: [{ 
             "CreatedBy": "Mickey Mouse", 
             "CreatedDate": "2015-08-17T13:16:22.713", 
             "CreatedDateDisplay": "17-08-2015", 
             "Description": "Test 1", 
             "Id": 7546798576985769857, 
             "Name": "Test 1", 
             "UpdatedBy": "", 
             "UpdatedDate": null, 
             "UpdatedDateDisplay": null, 
             "User": null, 
             "UserId": 0, 
             "Users": [{ 
              "Users": [{ 
               "Id": 7546798576985769858, 
               "UserId": 7546798576985769857, 
               "DisplayName": "Daffy Duck" 
              }, { 
               "Id": 7546798576985769859, 
               "UserId": 7546798576985769857, 
               "DisplayName": "Pluto" 
              }], 
              "User": "Bugs Bunny", 
              "UserId": 7546798576985769857, 
              "Name": "Test 2", 
              "Description": "Test 2", 
              "Id": 7546798576985769857, 
              "CreatedBy": "Goofy", 
              "CreatedDate": "2015-08-25T14:03:28.083", 
              "UpdatedBy": "Porky Pig", 
              "UpdatedDate": "2017-03-27T08:19:36.077", 
              "CreatedDateDisplay": "25-08-2015", 
              "UpdatedDateDisplay": "27-03-2017" 
             }] 
            }] 
           }]; 
          }); 
    </script> 
</html> 

希望すると、これが役立ちます。 ありがとう

0

ありがとうございました。ありがとうございました。私はGeethu Joseの答えを使用しましたが、それでも私が探していたものを正確に私に与えることはできませんでしたが、頭を掻き回した後、問題の解決策を考え出しました。配列のプレースホルダー[0]を使う代わりに、私はさまざまなレベルで配列自体にアクセスする必要があったので、コードを以下のように変更する必要がありました。

<div ng-app> 
    <div ng-controller="myController"> 
     <h1>{{helloMessage}}</h1> 
     <table> 
      <tbody> 
       <tr ng-repeat="data in Data"> 
        <td> 
         <table ng-repeat="records in data"> 
          <thead> 
           <tr> 
            <th>UserId</th> 
            <th>User</th> 
            <th>Name</th> 
           </tr> 
          </thead> 
          <tbody> 
           <tr> 
            <td>{{records.UserId}}</td> 
            <td>{{records.User}}</td> 
            <td>{{records.Name}}</td> 
            <td> 
             <table> 
              <thead> 
               <tr> 
                <th>Id</th> 
                <th>UserId</th> 
                <th>DisplayName</th> 
               </tr> 
              </thead> 
              <tr ng-repeat="streamUser in records.Users"> 
               <td>{{streamUser.Id}}</td> 
               <td>{{streamUser.UserId}}</td> 
               <td>{{streamUser.DisplayName}}</td> 
              </tr> 
             </table> 
            </td> 

           </tr> 
          </tbody> 
         </table> 
        </td> 
       </tr> 
      </tbody> 
     </table> 
    </div>