ngIf 指令(它與 AngularJS 1.x 中的 ng-if
指令的功能是等價)this
<div *ngIf="condition">...</div>
ngFor 指令(它與 AngularJS 1.x 中的 ng-repeat
指令的功能是等價的)spa
<li *ngFor="let item of items;">...</li>
ngIf 與 ngFor 指令使用示例code
import { Component } from '@angular/core'; @Component({ selector: 'sl-user', template: ` <div *ngIf="showSkills"> <ul> <li *ngFor="let skill of skills"> {{skill}} </li> </ul> </div> ` }) export class UserComponent { showSkills: boolean; skills: string[]; constructor() { this.showSkills = true; this.skills = ['AngularJS 1.x', 'Angular 2.x', 'Angular 4.x']; } }