我正在使用简单的ng-repeat
来生成一个国家列表。在每个列表中都有一个可以展开和折叠的隐藏行/ div。
我遇到的问题是,在将Angular引入我的应用程序之前,我手动硬编码了元素的ID,例如:
<li data-show="#country1">
{{country.name}} has population of {{country.population}}
<div id="country1">
<p>Expand/collapse content
</div>
</li>
<li data-show="#country2">
{{country.name}} has population of {{country.population}}
<div id="country2">
<p>Expand/collapse content
</div>
</li>
<li data-show="#country3">
{{country.name}} has population of {{country.population}}
<div id="country3">
<p>Expand/collapse content
</div>
</li>
<li data-show="#country4">
{{country.name}} has population of {{country.population}}
<div id="country4">
<p>Expand/collapse content
</div>
</li>
使用 ng-repeat
的新代码:
<li ng-repeat="country in countries" data-show="????">
{{country.name}} has population of {{country.population}}
<div id="???">
<p>Expand/collapse content
</div>
</li>
如何在我的ng-repeat
中分配动态/递增的ID?
$index
有时可能不够健壮,例如在读取数组时修改它(https://dev59.com/Smkw5IYBdhLWcg3wdKQv)。您还有哪些情况考虑到了吗? - Eduard Gamonal