一、指令

ngFor

  1. <div *ngFor="let item of arr">{{item.name}}</div>
  2. export class HeaderComponent implements OnInit {
  3. public arr:object [] = [{name:"html",age:10},{name:"css",age:30}]
  4. public isShow:boolean = true;
  5. }

获取多个值

  1. <li *ngFor="let item of todos, let i = index" [ngClass]="{completed: item.done}">
  2. <div class="view" >
  3. <button class="destroy" (click)="deleteTodo(i)"></button>
  4. </div>
  5. <input class="edit" value="Create a TodoMVC template">
  6. </li>

ngIf

  1. <div *ngIf="isShow">你好</div>
  2. export class HeaderComponent implements OnInit {
  3. public arr:object [] = [{name:"html",age:10},{name:"css",age:30}]
  4. public isShow:boolean = true;
  5. }

ngSwitch

  1. <div [ngSwitch]="status">
  2. <div *ngSwitchCase="1">1</div>
  3. <div *ngSwitchCase="2">2</div>
  4. <div *ngSwitchDefault>默认</div>
  5. </div>

ngClass

语法规则:

  1. <div [ngClass]="{
  2. css类名: 布尔值,
  3. css类名: 布尔值
  4. }">测试文本</div>
  1. <p [ngClass]="{'red': flag}">header works!</p>
  2. //flag为true,则添加class为red
  1. <p [ngClass]="{'red': flag,'blue':!flag}">header works!</p>
  2. //flag为true则class添加red,false则添加blue

小练习 list中的偶数项目添加class

  1. public list:any []=[{name:"lisi"},{name:"zhangsan"},{name:"wangwu"}]
  1. <div *ngFor="let item of arr;let key=index" [ngClass]="{'red':key%2==0}">
  2. {{key}}-{{item.name}}
  3. </div>

ngStyle

  1. <div [ngStyle]="{'color': 'red'}">header</div>

动态绑定样式

  1. public attr:string = "red"
  1. <div [ngStyle]="{'color': attr}">header</div>

二、属性绑定

  1. <img [src]="imageUrl" >
  2. or
  3. <img src="{{imageUrl}}">