简体   繁体   English

如何使用AngularJS的ngRepeat指令从嵌套的JSON对象动态生成HTML表?

[英]How to generate a HTML table dynamically from a nested JSON object using ngRepeat directive of AngularJS?

I have to create a table as follows from a nested object. 我必须从嵌套对象创建表,如下所示。

JSON object : JSON对象:

{
    "A":{
        "1":"X",
        "2":"Y",
        "3":"Z"
    },
    "B":{
        "1":"P",
        "2":"Q",
        "3":"R"
    }
}

Desired Table : 所需表:

A: X|Y|Z
B: P|Q|R

HTML code : HTML代码:

    <!DOCTYPE html>
<html data-ng-app="myApp">
<body>

    <div ng-controller="GreetingController" >
        <div ng-repeat='(key,val) in arr'>{{temp(val)}} 
            <div ng-repeat='(nestedKey,nestedVal) in aux'>  
                {{nestedVal}}
            </div>
        </div>
    </div>
</body>

<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js"></script>
<script>
    var myApp = angular.module('myApp',[]);
    myApp.controller('GreetingController', ['$scope', function($scope) {
        $scope.arr={"A":{"1":"X","2":"Y","3",},"B":{"1":"P","2":"Q"}};
        $scope.temp = function(val)
        {
            console.log(val);
            $scope.aux = val;
        }
    }]);
</script>

I am unable to understand how and where to place the <table> , <tr> and <td> tags to be able to generate the table as describe. 我无法理解如何以及在何处放置<table><tr><td>标记,以便能够按照描述生成表。

EDIT: 编辑:

tr is row and td is table cell tr是行, td是表单元格

<html data-ng-app="myApp">
<body>
    <div ng-controller="GreetingController" >
        <table>
            <tr ng-repeat='(key,val) in arr'>
                <td>
                    {{key}} 
                </td>
                <td ng-repeat='(nestedKey,nestedVal) in val'>  
                    {{nestedVal}}
                </td>
            </tr>
        </table>
    </div>
</body>
<div ng-controller="GreetingController" >
   <table>   
      <tr>
          <td><div ng-repeat='(key,val) in arr'>{{temp(val)}} </td>
      </tr>
      <tr><td> <div ng-repeat='(nestedKey,nestedVal) in aux'> </td> 
                 {{nestedVal}}
             </div>
      </tr>
        </div>
  </table>
 </div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用angularjs动态加载html表中的嵌套数组属性? - How to dynamically load the nested array attributes in html table using angularjs? 使用 Javascript 从 JSON 对象动态生成引导网格 HTML - Dynamically generate bootstrap grid HTML from JSON object using Javascript 如何使用任何JSON对象动态生成HTML FORM? - how to generate HTML FORM dynamically using any JSON object? angularjs 指令 ngrepeat 不适用于角度向导中的对象 - The angularjs directive ngrepeat is not working with object in angular wizard 如何使用AngularJs从表生成JSON以保存表格数据? - How to generate JSON from table using AngularJs for saving tabular data? 如何使用angular2从json对象动态生成TextFields - How to generate TextFields dynamically from json Object using angular2 AngularJS指令:将动态创建的对象作为嵌套指令的属性传递 - AngularJS Directive: Pass dynamically created object as attribute for nested directive 从 JSON object 生成 HTML 表 - Generate HTML table from JSON object 如何使用 Javascript 从带有嵌套列表的 JSON 动态创建表? - How to dynamically create a table from a JSON with nested lists using Javascript? AngularJS curstom指令和ngRepeat - AngularJS curstom directive and ngRepeat
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM