gpt4 book ai didi

angularjs - 使用嵌套 NG-Repeat 的序列号

转载 作者:行者123 更新时间:2023-12-02 03:14:01 26 4
gpt4 key购买 nike

我想在每个问题中添加序列号

ng-repeat = "Table in Paper"
Display table
ng-repeat = "question in question"
(1) Display question
(2) Display question

我可以在问题中使用 $index。但是对于每个表,它都是从 1 开始的。

例如

Table 1

Question 1

Question 2

Table 2

Question 1

Question 2

但是我想要

Table 1

Question 1

Question 2

Table 2

Question 3

Question 4

我使用 $parent 和 $index。但不工作。

<div ng-repeat="section in Table">
<div ng-repeat="question in section.question">
<span >{{(Section[$parent.$index-1].question.length + $index + 1 )+
'. ' +
question
.Description}}</span>

</div>

最佳答案

试试这个。

var myApp = angular.module('myApp',[]);
myApp.controller('MyCtrl', ["$scope",function MyCtrl($scope) {
$scope.lastIndex = 0;
$scope.lastIndexes = [];
$scope.Table ={
"table1":
{
"question":["questionA","questionB","questionC","questionD"]
},
"table2":
{
"question":["questionA","questionB","questionC","questionD"]
},
"table3":
{
"question":["questionC","questionD","questionF"]
},
"table4":
{
"question":["questionC","questionD","questionE"]
}
};

$scope.setLastIndex = function(len){
$scope.lastIndex = $scope.lastIndex + len*1;
$scope.lastIndexes.push($scope.lastIndex);
}

}]);
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="myApp" ng-controller="MyCtrl">
<div ng-repeat="(key,value) in Table"
ng-init="setLastIndex(value.question.length)">
{{key}}
<div ng-repeat="question in value.question" >
<span>{{($index+1 +lastIndexes[$parent.$index-1])}} :{{question}}</span>
</div>

</div>

关于angularjs - 使用嵌套 NG-Repeat 的序列号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38139905/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com