Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/visual-studio-2008/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
AngularJS从数组创建表_Angularjs - Fatal编程技术网

AngularJS从数组创建表

AngularJS从数组创建表,angularjs,Angularjs,我有一个项目数组,我想将其呈现为一个包含2列的表 我做了一个基本的实现,只使用一列进行渲染。有什么建议吗 <body ng-app="myApp" ng-controller="myCtrl"> <table> <tr ng-repeat="i in items"> <td>{{i}}</td> </tr> </table> </body> var myApp

我有一个项目数组,我想将其呈现为一个包含2列的表

我做了一个基本的实现,只使用一列进行渲染。有什么建议吗

<body ng-app="myApp" ng-controller="myCtrl">
  <table>
    <tr ng-repeat="i in items">
      <td>{{i}}</td>
    </tr>
  </table>
</body>


var myApp = angular.module("myApp", []);

myApp.controller("myCtrl", function($scope) {
  $scope.items = ["12", "22", "34", "657", "129"];
});

{{i}
var myApp=angular.module(“myApp”,[]);
控制器(“myCtrl”,函数($scope){
$scope.items=[“12”、“22”、“34”、“657”、“129”];
});

这是因为您的HTML只有一个
元素,您不会重复它。因为只有1列,所以只有1列。您需要在
元素中有一个嵌套的
ng repeat
,以获得多于一列,或者在HTML中明确定义两个
元素

您可以尝试编写更复杂的内容来确定何时应创建新的列或行,但我会通过将数组创建为更易于使用的内容来简化事情:基本上是一个二维数组。以下是我将要做的:

<body ng-app="myApp">
    <div ng-controller="myCtrl">
        <table>
            <tr ng-repeat="row in items">
                <td ng-repeat="column in row">{{column}}</td>
            </tr>
        </table>
    </div>
</body>

请注意,如果这些数组中的任何一个包含2个以上的值,那么您还将看到包含该数据的行的额外列

另一种方法是这样的,只保证2列。您需要为
项目
对象创建一个对象数组。像这样:

<body ng-app="myApp">
    <div ng-controller="myCtrl">
        <table>
            <tr ng-repeat="row in items">
                <td>{{row.column1}}</td>
                <td>{{row.column2}}</td>
            </tr>
        </table>
    </div>
</body>

我在谷歌上搜索并找到了答案。这就是我使用索引得出的结论

<body ng-app="myApp" ng-controller="myCtrl">
  <table>
    <tr ng-repeat="i in items" ng-if="$index%7 == 0">
      <td>{{items[$index]}}</td>
      <td>{{items[$index+1]}}</td>
      <td>{{items[$index+2]}}</td>
      <td>{{items[$index+3]}}</td>
      <td>{{items[$index+4]}}</td>
      <td>{{items[$index+5]}}</td>
      <td>{{items[$index+6]}}</td>
    </tr>
  </table>
</body>

// the main (app) module
var myApp = angular.module("myApp", []);

// add a controller
myApp.controller("myCtrl", function($scope) {
  $scope.items = ['12', '22', '34', '657', '129', '11', '23', '45', '65', '9', '76', '87', '90', '33', '51'];
});

{{items[$index]}
{{items[$index+1]}
{{items[$index+2]}
{{items[$index+3]}
{{items[$index+4]}
{{items[$index+5]}
{{items[$index+6]}
//主(应用程序)模块
var myApp=angular.module(“myApp”,[]);
//添加控制器
控制器(“myCtrl”,函数($scope){
$scope.items=['12','22','34','657','129','11','23','45','65','9','76','87','90','33','51'];
});

列是什么?请添加第二列:
{{{whatyouwanttodisplayintesecondcolumn}}
。但是由于您在一个字符串数组上循环,我想知道您可能希望在第二列中显示什么。这是可行的,但显示了一个严重的设计问题。您不应该使用字符串数组,而应该使用对象数组,其中每个对象都有命名字段:firstName、lastName,等等。使用
{{user.lastName}}
{{items[$index+1]}}
更清晰,不是吗?不仅仅是可读性方面;这个设计还有一个缺陷。如果您检查HTML输出,您将在每个“已使用”输出之间看到6个空
tr
<如果导致它们被注释掉,则代码>ng,但它们仍然存在。
var myApp = angular.module("myApp", []);

myApp.controller("myCtrl", function($scope) {
    $scope.items = [];
    $scope.items[0] = {};
    $scope.items[0].column1 = "12";
    $scope.items[0].column2 = "22";
    $scope.items[1] = {};
    $scope.items[1].column1 = "34";
    $scope.items[1].column2 = "657";
    $scope.items[2] = {};
    $scope.items[2].column1 = "129";
    $scope.items[2].column2 = null;
});
<body ng-app="myApp" ng-controller="myCtrl">
  <table>
    <tr ng-repeat="i in items" ng-if="$index%7 == 0">
      <td>{{items[$index]}}</td>
      <td>{{items[$index+1]}}</td>
      <td>{{items[$index+2]}}</td>
      <td>{{items[$index+3]}}</td>
      <td>{{items[$index+4]}}</td>
      <td>{{items[$index+5]}}</td>
      <td>{{items[$index+6]}}</td>
    </tr>
  </table>
</body>

// the main (app) module
var myApp = angular.module("myApp", []);

// add a controller
myApp.controller("myCtrl", function($scope) {
  $scope.items = ['12', '22', '34', '657', '129', '11', '23', '45', '65', '9', '76', '87', '90', '33', '51'];
});