Angular ng-repeat in table

家住魔仙堡 提交于 2019-12-13 07:10:39

问题


I've a question about angular 'ng-repeat'. I have a table, and in this table I will show the array values.

Here is my js:

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


app.controller('MainCtrl',function($scope){

        <!--TR-->
        $scope.trZeile = new Array();
        $scope.trZeile = ['1','2','3'];


        $scope.hadi = new Array();
        $scope.hadia = new Array();
        $scope.hadi[0] = 'Mercedes1,BMW1,Ford1,VW1,Renault1,Kia1';
        $scope.hadi[1] = 'Mercedes2,BMW2,Ford2,VW2,Renault2,Kia2';
        $scope.hadi[2] = 'Mercedes3,BMW3,Ford3,VW3,Renault3,Kia3';

        for(var i = 0; i<3; i++){
            $scope.hadia = $scope.hadi[i].split(',');
        }

});

Here is an example: http://plnkr.co/edit/jWqyePLEvrnwFZOkFUUn

But it should looks like:

How can I achieve that?


回答1:


You have a lot of unnecessary things here. I chopped them out. You don't need trZeile at all. Also, this line was simply rebinding the same variable three times:

$scope.hadia = $scope.hadi[i].split(',');

Here's the working code: http://plnkr.co/edit/zz04pEDPst5Yo3thCXfy?p=preview

HTML

<table ng-controller="MainCtrl" style='border:2px solid black'>
    <tr ng-repeat="row in hadia" style='border:2px solid black'>
       <th scope="row">{{$index + 1}}</th>
         <td ng-repeat="td in row track by $index" style='border:2px solid black'>
             {{td}}
          </td>
     </tr>
</table>

Javascript

var app = angular.module('plunker', []);
app.controller('MainCtrl',function($scope){
    $scope.hadia = [];
    $scope.hadi = [
       'Mercedes1,BMW1,Ford1,VW1,Renault1,Kia1',
       'Mercedes2,BMW2,Ford2,VW2,Renault2,Kia2',
       'Mercedes3,BMW3,Ford3,VW3,Renault3,Kia3'
    ]

    for(var i = 0, j = $scope.hadi.length; i< j; i++){
        $scope.hadia.push($scope.hadi[i].split(','));
    }
});


来源:https://stackoverflow.com/questions/29515847/angular-ng-repeat-in-table

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!