Javascript 嵌套中继器中项目的AngularJS增量id值
我有下面的代码,其中有问题和选项,我需要这些选项来增加id值 但是,每次出现新问题时,我都需要重新启动增量。到目前为止,我发现的大多数示例都是递增的(例如,使用$index)。但我在这里看了几篇文章,比如,没有得到我需要的结果: 我需要的是:Javascript 嵌套中继器中项目的AngularJS增量id值,javascript,html,angularjs,Javascript,Html,Angularjs,我有下面的代码,其中有问题和选项,我需要这些选项来增加id值 但是,每次出现新问题时,我都需要重新启动增量。到目前为止,我发现的大多数示例都是递增的(例如,使用$index)。但我在这里看了几篇文章,比如,没有得到我需要的结果: 我需要的是: Question 1 Choice10 (the first numerical value is the Id of the question, Choice11 the second numerical value should
Question 1
Choice10 (the first numerical value is the Id of the question,
Choice11 the second numerical value should be the incremented id)
Choice12
Question 2
Choice20
Choice21
Choice22
Choice23
html代码如下所示:
<table summary="" style="border: none; width: 100%; background: none;" id="rptQuestions">
<tbody>
<tr style="width: 100%;" ng-repeat-start="q in questions track by $index">
...Question details here...
</tr>
<tr>
<td class="Bold_10" colspan="4">
<table summary="" style="border: none; background: none">
<tbody>
<tr ng-repeat-start="c in choices" ng-if="c.QuestionId==q.QuestionId">
<td style="width: 2em;"><a href="javascript:void(0);" ng-click="DeleteChoice(c)">X</a></td>
<td>
<input type="text" id="inLetter{{c.QuestionId}}{{ value?? }}" ng-model="c.Letter" style="width: 2em;" /></td>
<td style="text-align: left;">
<input type="text" id="inChoice{{c.QuestionId}}{{ value?? }}" ng-model="c.Choice" style="width: 60em;" /> <input type="hidden" id="inChoiceId{{c.QuestionId}}{{ value?? }}" ng-value="{{c.Id}}" /></td>
</tr>
<tr ng-repeat-end ng-if="c.QuestionId==null"><td colspan="3"></td></tr>
</tbody>
</table>
</td>
</tr>
</tbody>
</table>
…这里的问题细节。。。
…这里的问题细节。。。
每个ng repeat都有自己的索引,您可以从嵌套循环访问索引。但是,请注意,在您的实现中,CHOICE111中的id可能会产生误导
问题1.11==问题11.1当我对选项进行$index时,问题1没有得到0,1,2,问题2没有得到0,1,2。第一个问题从0开始,第二个问题是3,4,5。可能是因为我使用了ng repeat start和ng repeat end吗?不确定,听起来很奇怪。您是否尝试删除ng repeat start?另外,您没有关闭第一个ng repeat start,也许最好将其删除。我还应该在索引号之间添加一个连字符,否则“父1,子11”将与“父11,子1”相同。@Xbirketx正在关闭,我忘了将该部分放在标记中。另外,我不能删除ng repeat start,因为问题和选项都是一个数组,我需要重复里面的所有元素。我最终决定这不是解决问题的方法,所以我添加了一个隐藏字段,其中包含一个类,该类具有每个选项的id值,我只是在javascript中对这些值进行for循环,这有助于消除重复id的问题,感谢您捕捉到这一点。
<table summary="" style="border: none; width: 100%; background: none;" id="rptQuestions">
<tbody>
<tr style="width: 100%;" ng-repeat-start="q in questions">
...Question details here...
</tr>
<tr ng-repeat-end="">
<td class="Bold_10" colspan="4">
<table summary="" style="border: none; background: none">
<tbody>
<tr ng-repeat-start="c in choices" ng-if="c.QuestionId==q.QuestionId">
<td style="width: 2em;"><a href="javascript:void(0);" ng-click="DeleteChoice(c)">X</a></td>
<td>
<input type="text" id="inLetter{{$parent.$index}}{{$index}}" ng-model="c.Letter" style="width: 2em;" /></td>
<td style="text-align: left;">
<input type="text" id="inChoice{{$parent.$index}}{{$index}}" ng-model="c.Choice" style="width: 60em;" /> <input type="hidden" id="inChoiceId{{$parent.$index}}{{$index}}" ng-value="{{c.Id}}" /></td>
</tr>
<tr ng-repeat-end ng-if="c.QuestionId==null"><td colspan="3"></td></tr>
</tbody>
</table>
</td>
</tr>
</tbody>
</table>