Angular, Directive *ngFor


Simple example of ngFor directive that is part of Angular core. Everything what you need will be generated after executing this command ng new hello-word. After that, what you have to do, is adjust few files. Of course more you can find in Angular documentation, here we have only my notes. One more thing, if you don’t like to install node mess on your local computer you can use my or similar docker image https://hub.docker.com/r/szalek/angular-cli/

app.component.ts

import { Component } from '@angular/core';

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent {
  title = 'app';

  computers = [
    {id: 1, name: 'com_ms_001'},
    {id: 2, name: 'com_ms_002'},
    {id: 3, name: 'com_ms_003'},
    {id: 4, name: 'com_ms_004'},
    {id: 5, name: 'com_ms_005'}
  ]
}

app.component.html

<ul>
  <li *ngFor="let com of computers; index as index; even as even; odd as odd; first as first; last as last">
    {{ index }} / {{ com.name }}
    <span *ngIf="even">even</span>
    <span *ngIf="odd">odd</span>
    <span *ngIf="first">first</span>
    <span *ngIf="last">last</span>
  </li>
</ul>