Angular에서 루프의 마지막 반복

*ngFor와 @for로 순회할 때 last 변수를 사용하여 루프의 마지막 반복을 정의할 수 있습니다.

*ngFor 디렉티브를 위한 예시:

app/app.component.html<ul>
  <li *ngFor="let el of arr; let last = last">
    {{ last ? '마지막' : '' }}
    {{ el }}
  </li>
</ul>

@for 구문을 위한 예시:

app/app.component.html<ul>
  @for (el of arr; track $index) {
    <li>
      {{ $last ? '마지막' : '' }}
      {{ el }}
    </li>
  }
</ul>
Задание 1

다음 배열이 주어졌습니다:

app/app.component.tsexport class AppComponent {
  public arr: string[] = ['a', 'b', 'c', 'd'];
}

배열의 요소를 ul 리스트로 출력하고, 마지막 li를 빨간색으로 칠하세요.

Задание 2

다음 배열이 주어졌습니다:

app/app.component.tsexport class AppComponent {
  public arr: string[] = ['a', 'b', 'c', 'd'];
}

배열의 요소를 ul 리스트로 출력하고, 첫 번째 li는 빨간색으로, 마지막은 초록색으로 칠하세요.