ng g c user-list
ng g c user-list -it
插值法css
{{}}
从数据流向划分为 3 种:html
绑定理解的误区express
<button [disabled]="isUnchanged">Save</button>安全
这并非将 isUnchanged 的值绑定到了 button 的 disabled attribute 上,而是设置在 该 DOM 元素的 property disabled 上。app
HTML attribute 与 DOM property 的对比函数
- HTML attribute 用来初始化 DOM property
- attribute 一旦设置则不可修改,property 能够修改
- attribute 能够理解为初始值,property 理解为当前值
- attribute 与 property 并非彻底对应的
模板绑定是经过 property 和 event 来工做的,而不是 attribte
性能
绑定的目标能够是 property、event、property+event(双向)字体
绑定目标汇总表
[property]
形式bind-property
形式不该该绑定一个会修改其余任何值的表达式(属性、函数),这可能会致使意料以外的问题ui
[hero]="currentHero" 与 hero="currentHero" 是不一样的this
- [hero]= 将 currentHero 表达式 绑定到 hero property
- hero= 将 currentHero 字面值 赋予 hero attribute
在绑定字符串类型值的时候 src="{{url}}" 等价于 [src]="url"
也写特殊的元素的 DOM 中并无 property 可供绑定,例如 <td colspan="{{ 1 + 1}}"></td>
元素 td 只有 colspan attribute,而没有其对应的 property,这时候就只能使用 attribute bind。
使用 attr.
绑定 attribute,例如:
<td [attr.colspan]="1 + 1"></td>
[class.myClass]="expression",表达式为真,添加类,反之移除。
[class]="expression" 表达式应该返回一个包含类名的字符串。
[ngClass]="{ 'myClass1': true, 'myClass2': isClass2 }"
<span [style.color]="fontColor">字体颜色</span>
[ngStyle]="{ 'font-color': '#333', 'font-weight': '700' }"
<button (click)="onClick()">click</button>
<button (click)="onClick()">click</button>
一般,在 angular 中,经过 $event事件对象
传递事件信息 ,例如这样:
<input [value]="name" (input)="name=$event.target.value">
这其实就至关于一个 双向绑定
啦。
经过 EventEmitter 能够自定义事件,例如:
假设咱们有一个对话框组件,他有 ok 和 cancel 两个按钮:
app-dialog
// html... <button (click)="ok()"></button> <button (click)="cancel()"></button> // ts... @Output('onConfirm') confirmEvent = new EventEmitter<any>(); @Output('onCancel') cancelEvent = new EventEmitter<any>(); public ok():void { this.confirmEvent.emit("确认"); } public cancel():void { this.cancelEvent.emit("取消"); }
调用方
<app-dialog (onConfirm)="func1($event)" (onCancel)="func2($event)"></app-dialog>
$event 就是 emit 传递过来的值
使用 ngModel
指令须要 FormsModule
,因此须要提早将 FormsModule 添加到 imports 当中。
双向绑定就是 属性绑定与事件绑定 同时修饰同一个 property,至少 bey 是这么理解的。
假设咱们有一个组件,他接受一个叫作 value 的 property,而且对外提供了一个 value 的 change 事件,那么能够这么写到:
<app-value [value]="myvalue" (onValueChange)="myvalue=$event">
angular 以为这样写挺麻烦的,因而搞了一个 语法糖
,结果语句就变成了这样:
<app-value [(value)]="myvalue">
指令分为两种: 属性型指令 和 结构型指令。
属性型指令通常用来处理 property、attribute、event。例如:NgClass、NgStyle、NgModel。
ngModel 指令经过本身的输入属性 ngModel 和输出属性 ngModelChange 实现双向绑定,简化以后的
语法糖
就是 [(ngModel)]。
结构型指令一般会改变 DOM 结构;例如 NgIf、NgSwitch、NgForOf
ngIf 与 [style.hidden] 是不一样的,这与 Vue 的 v-if 与 v-show 相似。ngIf 会从 DOM 移除元素,而 [style.hidden] 仅仅隐藏元素。
1. 微语法
模板输入变量
*ngFor="let hero of heroes" [hero]="hero",let 关键字建立了一个叫作 hero 的 模板输入变量。他的做用域包括宿主元素和其子元素。
2. *ngFor 索引
*ngFor="let hero of heroes; let i=index"
3. *ngFor trackBy
trackBy 用来告诉 *ngFor 如何避免不该该的性能消耗(避免重复的渲染没有变化的项)
使用 trackBy 须要告诉他须要追踪的值,例如:
<div *ngFor="let hero of heroes; trackBy: trackByHeroes"> ({{hero.id}}) {{hero.name}} </div> //... trackByHeroes(index: number, hero: Hero): number { return hero.id; }
只要 id 没有发生变化,DOM 就不会重复劳动。
4. NgSwitch
实际上 NgSwitch 包括 3 个指令:
用法:
<div [ngSwitch]="currentHero.emotion"> <app-happy-hero *ngSwitchCase="'happy'" [hero]="currentHero"></app-happy-hero> <app-sad-hero *ngSwitchCase="'sad'" [hero]="currentHero"></app-sad-hero> <app-confused-hero *ngSwitchCase="'confused'" [hero]="currentHero"></app-confused-hero> <app-unknown-hero *ngSwitchDefault [hero]="currentHero"></app-unknown-hero> </div>
NgSwitch 是一个 属性指令,他只修改了行为,没有修改结构,因此不须要 *
5. 模板引用变量
语法:
#var 或 ref-var
模板引用变量一般用来引用模板中的某个 DOM
或 Angular组件
或 指令
或 Web Component
。
模板引用变量的做用域是本模板全局。
<input #phone placeholder="phone number">
<form (ngSubmit)="onSubmit(heroForm)" #heroForm="ngForm"> <div class="form-group"> <label for="name">Name <input class="form-control" name="name" required [(ngModel)]="hero.name"> </label> </div> <button type="submit" [disabled]="!heroForm.form.valid">Submit</button> </form> <div [hidden]="!heroForm.form.valid"> {{submitMessage}} </div>
对比 | 模板引用变量 | 模板输入变量 |
---|---|---|
声明方式 | #var/ref-var | let var |
做用域 | 整个模板 | 宿主、子元素 |
1. 输入属性
带有 @Input
装饰器的 可设置属性,通常用于 属性绑定。
@Input('width'),本组件接受一个叫作 width 的属性绑定。例如: <app-component [width]="width"></app-component>
2. 输出属性
带有 @Output
装饰器的 可观察对象 属性,通常用于 输出事件。
@Output('onClick') clicks = new EventEmitter<string>();
对外提供一个可供绑定的名为 onClick
的事件,该事件的参数是一个 string
。
1. 管道操做符(|)
管道是一个函数,适合在模板表达式中,处理包括:数据显示格式、过滤、排序这样的操做;该函数接受一个输入值,返回结果。
通常用法
<div>Hello {{world | uppercase}}</div>
管道函数能够是连续的使用:
<div>Hello {{wORld | lowercase | uppercase}}</div>
传参:
<div>Birthdate: {{currentHero?.birthdate | date:'longDate'}}</div>
2. 安全导航操做符(?.)
观看该案例:
<span>{{person.name}}</span>
当 person 为 undefined 时,该代码会报错。修改成以下后不会报错:
<span>{{person?.name}}</span>
3. 非空断言操做符(!)
{{person!.name}}
只有在开启了 TypeScript 严格空值检查时,才会用到。
当表达式没法检测出某个对象包含指定的字段(属性)时,可使用 $any 让其不进行 TypeScript 的类型编译。