programing

AngularJS를 사용하여 'ng-repeat' 항목의 인덱스(카운터)를 얻을 수 있습니까?

easyjava 2023. 3. 5. 10:30
반응형

AngularJS를 사용하여 'ng-repeat' 항목의 인덱스(카운터)를 얻을 수 있습니까?

Angular를 사용하고 있습니다.JS와 그ng-repeat일련의 질문을 표시하도록 지시합니다.각 질문에 번호를 매겨야 합니다.1이러한 카운터를 표시 및 증가시키는 방법ng-repeat지금까지 알아낸 내용은 다음과 같습니다.

<ul>
    <li ng-repeat="question in questions | filter: {questionTypesId: questionType, selected: true}">
        <div>
            <span class="name">
                {{ question.questionText }}
            </span>
        </div>
        <ul>
            <li ng-repeat="answer in question.answers">
                <span class="name">
                    {{answer.selector}}. {{ answer.answerText }}
                </span>
            </li>
        </ul>
    </li>
</ul>

Angularjs 문서에는 예가 가득합니다.잠시 시간을 두고 살펴보시기 바랍니다.

다음 예시를 참조하십시오.ngRepeat 예, 같은 경우입니다.

<ul>
    <li ng-repeat="question in questions | filter: {questionTypesId: questionType, selected: true}">
        <div>
            <span class="name">
                {{$index + 1}} {{ question.questionText }}
            </span>
        </div>
        <ul>
            <li ng-repeat="answer in question.answers">
                <span class="name">
                    {{answer.selector}}. {{ answer.answerText }}
                </span>
            </li>
        </ul>
    </li>
</ul>

비슷한 걸 찾아서 여기까지 왔어

여기 나에게 효과가 있었던 것이 있다.

{{$index + 1}}

인덱스가 0부터 시작되므로 +1이 추가됩니다.

지정된 어레이의 인덱스를 ng-repeat으로 가져오려면 두 가지 방법이 있습니다.

$index 사용

<th ng-repeat="n in [].constructor(3 + 1) track by $index">{{$index}}</th>

카운터 사용

이 메서드는 ng-repeat이 네스트되어 있어 양쪽 루프의 카운터가 필요한 경우에 도움이 됩니다.다음 예제에서는 카운터 행을 사용합니다.$index에 의한 트랙이 필요하지만 로직에는 사용되지 않습니다.

<tr ng-repeat="(row, y) in getNumber(h.seats.length, 3) track by $index">
                    <td>{{row+1}}</td>

두 경우 모두 카운터가 0으로 시작되며 +1을 사용하여 오프셋할 수 있습니다.

언급URL : https://stackoverflow.com/questions/22335095/get-the-index-counter-of-an-ng-repeat-item-with-angularjs

반응형