angular4筆記系列之內(nèi)置指令小結(jié)
內(nèi)置指令
內(nèi)置屬性型指令
屬性型指令會(huì)監(jiān)聽和修改其它HTML元素或組件的行為、元素屬性(Attribute)、DOM屬性(Property)。
NgClass
形式:[ngClass]="statement"
通過(guò)綁定到NgClass,可以同時(shí)添加或移除多個(gè)類。
setCurrentClasses() { this.currentClasses = { 'saveable': this.canSave, 'modified': !this.isUnchanged, 'special': this.isSpecial }; } <div [ngClass]="currentClasses">This div</div>
NgStyle
形式:[ngStyle]="statement"
NgStyle綁定可以同時(shí)設(shè)置多個(gè)內(nèi)聯(lián)樣式。
setCurrentStyles() { this.currentStyles = { 'font-style': this.canSave ? 'italic' : 'normal', 'font-weight': !this.isUnchanged ? 'bold' : 'normal', 'font-size': this.isSpecial ? '24px' : '12px' }; } <div [ngStyle]="currentStyles">This div</div>
NgModel
形式:[(ngModel)]="statement"
使用[(ngModel)]雙向綁定到表單元素。
<input [(ngModel)]="currentHero.name">
使用 ngModel 時(shí)需要 FormsModule
內(nèi)置結(jié)構(gòu)型指令
NgIf
形式:*ngIf="statement"
<app-hero-detail *ngIf="isActive"></app-hero-detail>
NgFor
形式:*ngFor="statement"
<div *ngFor="let hero of heroes">{{hero.name}}</div>
NgSwitch
形式:[ngSwitch]="statement"
<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-unknown-hero *ngSwitchDefault [hero]="currentHero"></app-unknown-hero> </div>
NgSwitch實(shí)際上包括三個(gè)相互協(xié)作的指令:NgSwitch、NgSwitchCase 和 NgSwitchDefault
模板引用變量 ( #var )
模板引用變量通常用來(lái)引用模板中的某個(gè)DOM元素,它還可以引用Angular組件或指令或Web Component。
<input #phone placeholder="phone number"> <button (click)="callPhone(phone.value)">Call</button>
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
在Angular中使用NgTemplateOutlet創(chuàng)建可重用組件的流程步驟
在 Angular 中,使用 NgTemplateOutlet 而不是創(chuàng)建特定組件,可以使組件在不修改組件本身的情況下輕松修改為各種用例,在本文中,您將接受一個(gè)現(xiàn)有組件并重寫它以使用 NgTemplateOutlet,需要的朋友可以參考下2024-03-03Angular 2父子組件數(shù)據(jù)傳遞之@Input和@Output詳解 (上)
這篇文章主要給大家介紹了關(guān)于Angular 2父子組件數(shù)據(jù)傳遞之@Input和@Output的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面跟著小編一起來(lái)看看吧。2017-07-07Angular JS 生成動(dòng)態(tài)二維碼的方法
這篇文章主要介紹了Angular JS 生成動(dòng)態(tài)二維碼的方法,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2017-02-02Angularjs Ng_repeat中實(shí)現(xiàn)復(fù)選框選中并顯示不同的樣式方法
今天小編就為大家分享一篇Angularjs Ng_repeat中實(shí)現(xiàn)復(fù)選框選中并顯示不同的樣式方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-09-09Angular實(shí)現(xiàn)一個(gè)簡(jiǎn)單的多選復(fù)選框的彈出框指令實(shí)例
下面小編就為大家?guī)?lái)一篇Angular實(shí)現(xiàn)一個(gè)簡(jiǎn)單的多選復(fù)選框的彈出框指令實(shí)例。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-04-04AngularJS實(shí)現(xiàn)Input格式化的方法
這篇文章主要介紹了AngularJS實(shí)現(xiàn)Input格式化的方法,結(jié)合實(shí)例形式分析了AngularJS實(shí)現(xiàn)Input格式化的操作步驟與相關(guān)注意事項(xiàng),需要的朋友可以參考下2016-11-11