반응형
AngularJS - 새로운 고유 ID를 할당/생성하기 위한 ng-repeat
심플을 사용하고 있습니다.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>
동적/증분 ID를 할당하려면ng-repeat?
사용할 수 있습니다.$index https://docs.angularjs.org/api/ng/directive/ngRepeat
<li ng-repeat="country in countries" data-show="????">
{{country.name}} has population of {{country.population}}
<div id="country-{{$index}}">
<p>Expand/collapse content
</div>
</li>
ng-repeat이 네스트 되었을 때 다음과 같은 것이 필요할 수 있습니다.
<label id="country-{{$parent.$index}}-{{$index}}" ng-repeat="city in country.cites"> {{city.name}} </label>
{{$index+1}}페이지 번호에 따라 일련 번호를 변경하려면 페이지 번호의 모든 페이지에 대해 1-5를 표시합니다.{{$index+curPage*5+1}}여기서 curPage는 페이지 번호의 현재 페이지입니다.
언급URL : https://stackoverflow.com/questions/23264789/angularjs-ng-repeat-to-assign-generate-a-new-unique-id
반응형
'sourcecode' 카테고리의 다른 글
| React 구성 요소 디렉토리에서 index.js 파일은 어떻게 작동합니까? (0) | 2023.03.14 |
|---|---|
| HikariCP 스프링 부트 로깅 (0) | 2023.03.09 |
| ng-model이 텍스트 입력 후 업데이트되지 않음 (0) | 2023.03.09 |
| Wordpress에서 ajax와 함께 양식 제출 (0) | 2023.03.09 |
| jQuery UI에서 여러 정렬 가능한 목록을 서로 연결하려면 어떻게 해야 합니까? (0) | 2023.03.09 |