一、属性绑定 [ ]
<img [src]="imageUrl" >
二、指令ngFor,ngIf
vscode插件:Angular Snippets (Version 8)
<div *ngFor="let item of arr">{{item.name}}</div>
<div *ngIf="isShow">你好</div>
三、事件
3-1 (click)
//.html
<p (click)="handleClick()">{{msg}}</p>
//.component.ts
export class HeaderComponent implements OnInit {
....
public msg:string ="我喜欢写代码"
constructor() { }
ngOnInit() {
}
handleClick(){
this.msg = "change"
}
}
3-2(keyup)
//.html
<input type="text" (keyup)="handleEnter($event)">
//.component.ts
handleEnter(event:any){
console.log(event.keyCode)
}
3-3 (keyup.enter)
//component.ts
handleEnter(value:string){
console.log(value)
}
四、双向数据绑定
4-1app.module.ts中配置
import {FormsModule} from '@angular/forms'
imports: [
BrowserModule,
FormsModule
],
4-2 组件中使用
<input type="text"
[(ngModel)]="msg">
4-4 (change)
//.html
<input type="text"
(change)="handleChange($event)"
[(ngModel)]="msg">
//component.ts
handleChange(e){
console.log(e.target)
}
4-5 (ngModelChange)
只要ngModel依赖的值改变的时候,就会触发
ngModel一定要写在ngModelChange的前面
//.html
<input type="checkbox"
[(ngModel)]="checked"
(ngModelChange)="handleChange()"
>
//.component.ts
public checked:boolean = true;
handleChange(){
console.log(this.checked);
}
五、ant-design
自动构建
ng add ng-zorro-antd