ngFor with Angular 2 and TypeScript

ngFor is a repeater directive which outputs a list of elements by iterating over an array.

Updated Jan 2017, Angular version 2.4.x

Example

In the code snippet below we use the ngFor directive to iterate over the people array and store each item in a template variable called person. This variable is then accessed within the template using interpolation. We highlight every other row by applying a CSS class when the row is odd. Angular provides an even value too.

In the second ngFor statement we use a function to generate an array to loop over. index provides a zero-based incrementing value for each item in the array. last identifies the last item in the array allowing us to omit the final comma. Angular also provides the first value as you would expect.

ng-for.component.ts
import { Component } from '@angular/core';

@Component({
    selector: 'ng-for-directive',
    template: `
        <h2>ngFor</h2>
        
        <div *ngFor="let person of people; let isOdd=odd; trackBy:trackByPeople">
            <span [class.highlight]="isOdd">{{person.name.forename}} {{person.name.surname}}</span>
        </div>
        
        <h3>For Loop</h3>
        
        <span *ngFor="let item of forLoopArray(15); let i=index; let last=last">
            <span>{{i+1}}{{last ? '' : ','}}</span>
        </span>`,
    styles: ['.highlight {background-color: Lavender }']
})
export class NgForComponent {
    private people = [
            {id: 1, name: {forename: 'John', surname: 'Doe'}},
            {id: 2, name: {forename: 'John', surname: 'Smith'}},
            {id: 3, name: {forename: 'Peter', surname: 'Scott'}},
            {id: 4, name: {forename: 'Sue', surname: 'Reece'}}
        ];

    private trackByPeople(index: number, person: any) {
        return person.id;
    }
    private forLoopArray(elements: number): Array<any> {
        return new Array(elements);
    }
}

The Application

Here is the output from this component.

Where Next?

To find out more about Angular and TypeScript, check out these tutorials.

  • Hello World - Implement a super-simple <hello-world> custom element using an Angular and TypeScript.
  • The Angular with TypeScript Tutorial - includes examples of components, template syntax, property binding, event binding, bootstrapping and more.
  • Configuration - Configure Angular and TypeScript to download dependencies from node modules or a CDN, and to compile the TypeScript during development or in the browser at runtime.
  • Templates - introduction to inline and external templates.
  • Interpolation - use curly braces and template expressions to output data on the page.
  • Property Binding - bind to DOM properties using square brackets and template expressions.
  • Event Binding - handle DOM events using parentheses and template statements.
  • Two-way Binding - combine property and event binding to create two-way binding with ngModel.
  • Input Binding - bind to <input> fields such as text, textarea, checkbox, radio and select.
  • Built-in Directives - see how to use built-in directives ngIf, ngSwitch, ngFor, ngClass and ngStyle.
  • Component Input Output - use @Input and @Output to pass data in to and out of a component.
  • Angular Router - Use the Angular router to navigate between components when the user clicks a link.
  • Nested Child Routes - An example of how child routes allow navigation between multiple views when a user clicks a link in a sub-menu.