增加角度视图中的数字(级联ng重复)

时间:2016-06-14 06:01:53

标签: javascript angularjs angularjs-ng-repeat

我有类似的情况,如下面的例子所示,并希望打印独立于子,I.E的每一行的序列号。对于每个商标记录。

Java脚本对象结构:

 $scope.childsList = [
        {
        id:12346,
        name : 'Sagar Kulthe',
        marksList : [{subject:'Math', marks: 55}, {subject:'English', marks: 54}, 
                        {subject:'Biology', marks: 65},     
                        {subject:'Physics', marks: 78}]
    },
    {
        id:12346,
        name: 'Amol Pawal',
        marksList : [{subject:'Math', marks: 55}, {subject:'English', marks: 54}, 
                        {subject:'Biology', marks: 65},     
                        {subject:'Physics', marks: 78}]
    },
    {
        id:12346,
        name: 'Tushar Shah', 
        marksList : [{subject:'Math', marks: 55}, {subject:'English', marks:      
                         54},  {subject:'Biology', marks: 65},
                         {subject:'Physics', marks: 78}]
    }
    ];

查看:

<table>
    <thead>
        <th>Sr NO:</th>
        <th>Name</th>
        <th>Subject</th>
        <th>Marks</th>
    </thead>
    <tbody ng-repeat="child in childsList track by $index">
        <tr ng-repeat="mark in marksList track by $index" ng-init="marksIndex = $index">
            <td>{{marksIndex }}</td>
            <td>{{child.name}}</td>
            <td>{{mark.subject}}</td>
            <td>{{mark.marks}}</td>
        </tr>
    </tbody>
</table>

3 个答案:

答案 0 :(得分:1)

以下方法对您有所帮助, https://plnkr.co/edit/CEbyJNevu94LBogYUek7?p=preview

<table ng-init="count[0]  = 0">
  <thead>
    <th>Sr NO:</th>
    <th>Name</th>
    <th>Subject</th>
    <th>Marks</th>
  </thead>
<tbody ng-repeat="child in childsList track by $index" ng-init="count[+$index + 1]  = +count[+$index] + child.marksList.length">

    <tr ng-repeat="mark in child.marksList track by $index">

        <td>{{count[+$parent.$index] + $index + 1}}</td>
        <td>{{child.name}}</td>
        <td>{{mark.subject}}</td>
        <td>{{mark.marks}}</td>

    </tr> 
</tbody>

答案 1 :(得分:0)

我想你必须改变这个:

<td>{{mark.subject}}</td>
<td>{{mark.marks}}</td>

此外,我认为您不需要ng-init指令,因为$index可以直接访问。所以,你可以改为:

根据其他已立即删除的答案的评论进行了更新

假设这是控制器:

app.controller('appCtrl', ['$scope', function($scope){
    $scope.counter = 0;
    $scope.childsList = [
      ......
    ];
}]);

现在在标记中

 <tbody ng-repeat="child in childsList track by $index">
    <tr ng-repeat="mark in marksList track by $index" ng-init="marksIndex = $index">
        <td>{{counter+1}}</td><!-- Will start as 1, 2, 3 -->
        <td>{{child.name}}</td>
        <td>{{mark.subject}}</td>
        <td>{{mark.marks}}</td>
    </tr>
</tbody>

答案 2 :(得分:0)

我只是举个例子,希望它可以帮助你 只需要改变数字2等于你的markList长度兄弟 https://embed.plnkr.co/vkizqh213NN6aUeB9tgQ/