1、ngFor,ngIf

  1. <div *ngFor="let item of arr">{{item.name}}</div>
  2. <div *ngIf="isShow">你好</div>
  1. //.ts
  2. export class HeaderComponent implements OnInit {
  3. public arr:object [] = [{name:'html',age:10},{name:"css",age:8}]
  4. public isShow:boolean = true;
  5. constructor() { }
  6. ngOnInit() {
  7. }
  8. }

2、属性绑定[src]

  1. <img [src]="imageUrl" alt="">
  1. //.ts
  2. export class HeaderComponent implements OnInit {
  3. public imageUrl:string = "http://b.hiphotos.baidu.com/image/h%3D300/sign=92afee66fd36afc3110c39658318eb85/908fa0ec08fa513db777cf78376d55fbb3fbd9b3.jpg"
  4. constructor() { }
  5. ngOnInit() {
  6. }
  7. }

3、事件

(click)

  1. <p (click)="handleClick()">{{msg}}</p>
  1. //.ts
  2. export class HeaderComponent implements OnInit {
  3. constructor() { }
  4. ngOnInit() {
  5. }
  6. handleClick(){
  7. this.msg = "change"
  8. }
  9. }

(keyup)—获取键盘码

  1. <input type="text" (keyup)="handleEnter($event)" />
  1. //.ts
  2. export class HeaderComponent implements OnInit {
  3. constructor() { }
  4. ngOnInit() {
  5. }
  6. handleEnter(event:any){
  7. console.log(event.keyCode)
  8. }
  9. }

(keyup.enter)—回车获取input输入框的值

  1. <input #box (keyup.enter)="handleEnter(box.value)" />
  1. //.ts
  2. export class HeaderComponent implements OnInit {
  3. constructor() { }
  4. ngOnInit() {
  5. }
  6. handleEnter(value:string){
  7. console.log(value)
  8. }
  9. }