16 April 2020

ngFor example in angular

  • Open Command prompt and use below commands to create angular project
  • E:
  • cd E:\TestProjects\Angular
  • ng new ngFor-example-in-angular --style=scss --routing --skip-install
  • cd ngFor-example-in-angular
  • npm install
  • Open project in visual studio code.
  • Open app.component.ts file and update code as below
  • import { Component } from '@angular/core';

    @Component({
      selector: 'app-root',
      templateUrl: './app.component.html',
      styleUrls: ['./app.component.scss']
    })
    export class AppComponent {
      Months:string[];
      ngOnInit()
      {
        this.Months=[
          "January","February","March","April","May","June","July","August","September","October","November","December"
        ]
      }
    }

  • Open app.component.html file and update code as below
  • <h1>ngFor example in angular</h1>

    <ul>
      <li *ngFor="let month of Months">
        {{month}}
      </li>
    </ul>

    Output:

    No comments:

    Post a Comment