表格中的角度ng-repeat [英] Angular ng-repeat in table

查看:25
本文介绍了表格中的角度ng-repeat的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个关于角度ng-repeat"的问题.我有一个表,在这个表中我将显示数组值.

这是我的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(',');}});

这是一个例子:

HTML

<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>

Javascript

var app = angular.module('plunker', []);app.controller('MainCtrl',function($scope){$scope.hadia = [];$scope.hadi = ['梅赛德斯 1、宝马 1、福特 1、大众 1、雷诺 1、起亚 1','梅赛德斯 2、宝马 2、福特 2、大众 2、雷诺 2、起亚 2','梅赛德斯3、宝马3、福特3、大众3、雷诺3、起亚3']for(var i = 0, j = $scope.hadi.length; i< j; i++){$scope.hadia.push($scope.hadi[i].split(','));}});

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?

解决方案

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(','));
    }
});

这篇关于表格中的角度ng-repeat的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆