建了個羣有興趣的朋友能夠加一下 QQ 羣:Angular 修仙之路(1)羣 - 153742079 (已滿),請加 Angular 修仙之路(2)羣 - 648681235。
<ng-template>
元素ngTemplateOutlet
指令本系列教程的開發環境及開發語言:html
npm install -g @angular/cli
ng new PROJECT-NAME
cd PROJECT-NAME ng serve
Angular 的指令分爲三種:git
DOM
元素來改變 DOM
佈局在 Angular 中,咱們能夠使用 HostBinding
裝飾器,實現元素的屬性綁定。github
該指令用於演示如何利用 HostBinding
裝飾器,設置元素的 innerText
屬性。typescript
import { Directive, HostBinding} from '@angular/core'; @Directive({ selector: '[greet]' }) export class GreetDirective { @HostBinding() innerText = 'Hello, Everyone!'; }
import { Component } from '@angular/core'; @Component({ selector: 'app-root', template: ` <h2>Hello, Angular</h2> <h2 greet>Hello, Angular</h2> `, }) export class AppComponent { }
爲了可以讓用戶自定義 GreetDirective
指令的問候內容,咱們須要使用 Input
裝飾器去定義指令的輸入屬性。npm
該指令用於演示如何利用 Input
裝飾器,定義指令的輸入屬性,從而實現讓用戶自定義問候內容。segmentfault
import { Directive, HostBinding, Input } from '@angular/core'; @Directive({ selector: '[greet]' }) export class GreetDirective { @Input() greet: string; @HostBinding() get innerText() { return this.greet; } }
import { Component } from '@angular/core'; @Component({ selector: 'app-root', template: ` <h2>Hello, Angular</h2> <h2 [greet]="'Hello, Semlinker!'">Hello, Angular</h2> `, }) export class AppComponent { }
在 Angular 中,咱們能夠使用 HostListener
屬性裝飾器,實現元素的事件綁定。服務器
該指令用於演示如何利用 HostListener
裝飾器,監聽用戶的點擊事件。app
import { Directive, HostBinding, HostListener, Input } from '@angular/core'; @Directive({ selector: '[greet]' }) export class GreetDirective { @Input() greet: string; @HostBinding() get innerText() { return this.greet; } @HostListener('click',['$event']) onClick(event) { this.greet = 'Clicked!'; } }
import { Component } from '@angular/core'; @Component({ selector: 'app-root', template: ` <h2>Hello, Angular</h2> <h2 [greet]="'Hello, Semlinker!'">Hello, Angular</h2> `, }) export class AppComponent { }
在 Angular 中,咱們能夠經過 Attribute
裝飾器來獲取指令宿主元素的屬性值。less
該指令用於演示如何利用 Attribute
裝飾器,獲取指令宿主元素上的自定義屬性 author
的值。佈局
import { Directive, HostBinding, HostListener, Input, Attribute } from '@angular/core'; @Directive({ selector: '[greet]' }) export class GreetDirective { @Input() greet: string; @HostBinding() get innerText() { return this.greet; } @HostListener('click',['$event']) onClick(event) { this.greet = 'Clicked!'; console.dir(event); } constructor(@Attribute('author') public author: string) { console.log(author); } }
import { Component } from '@angular/core'; @Component({ selector: 'app-root', template: ` <h2>Hello, Angular</h2> <h2 [greet]="'Hello, Semlinker!'" author="semlinker">Hello, Angular</h2> `, }) export class AppComponent { }
<ng-template>
元素在 Angular 中,咱們能夠經過 ViewChild
裝飾器來獲取視圖中定義的模板元素,而後利用 ViewContainerRef
對象的 createEmbeddedView()
方法,建立內嵌視圖。
import { Component, TemplateRef, ViewContainerRef, ViewChild, AfterViewInit } from '@angular/core'; @Component({ selector: 'app-root', template: ` <ng-template #tpl> Hello, Semlinker! </ng-template> `, }) export class AppComponent implements AfterViewInit{ @ViewChild('tpl') tplRef: TemplateRef<any>; constructor(private vcRef: ViewContainerRef) {} ngAfterViewInit() { this.vcRef.createEmbeddedView(this.tplRef); } }
ngTemplateOutlet
指令該指令用於基於已有的 TemplateRef
對象,插入對應的內嵌視圖。在應用 NgTemplateOutlet 指令時,咱們能夠經過 [ngTemplateOutletContext]
屬性來設置 EmbeddedViewRef
的上下文對象。綁定的上下文應該是一個對象,此外可經過 let
語法來聲明綁定上下文對象屬性名。
import { Component } from '@angular/core'; @Component({ selector: 'app-root', template: ` <ng-template #stpl> Hello, Semlinker! </ng-template> <ng-template #atpl> Hello, Angular! </ng-template> <div [ngTemplateOutlet]="atpl"></div> <div [ngTemplateOutlet]="stpl"></div> `, }) export class AppComponent { }
import { Component } from '@angular/core'; @Component({ selector: 'app-root', template: ` <ng-template #stpl let-message="message"> <p>{{message}}</p> </ng-template> <ng-template #atpl let-msg="message"> <p>{{msg}}</p> </ng-template> <ng-template #otpl let-msg> <p>{{msg}}</p> </ng-template> <div [ngTemplateOutlet]="atpl" [ngTemplateOutletContext]="context"> </div> <div [ngTemplateOutlet]="stpl" [ngTemplateOutletContext]="context"> </div> <div [ngTemplateOutlet]="otpl" [ngTemplateOutletContext]="context"> </div> `, }) export class AppComponent { context = { message: 'Hello ngOutletContext!', $implicit: 'Hello, Semlinker!' }; }
該指令實現 ngIf
指令相反的效果,當指令的輸入條件爲 Falsy
值時,顯示DOM元素。
import { Directive, Input, TemplateRef, ViewContainerRef } from '@angular/core'; @Directive({ selector: '[exeUnless]' }) export class UnlessDirective { @Input('exeUnless') set condition(newCondition: boolean) { if (!newCondition) { this.viewContainer.createEmbeddedView(this.templateRef); } else { this.viewContainer.clear(); } } constructor(private templateRef: TemplateRef<any>, private viewContainer: ViewContainerRef) { } }
import { Component } from '@angular/core'; @Component({ selector: 'app-root', template: ` <h2 *exeUnless="condition">Hello, Semlinker!</h2> `, }) export class AppComponent { condition: boolean = false; }
組件繼承於指令,並擴展了與 UI 視圖相關的屬性,如 template、styles、animations、encapsulation 等。
詳細內容請參考 - Angular 2 Directive Lifecycle
TemplateRef
與 ViewContainerRef
有什麼做用?TemplateRef:用於表示內嵌的 template 模板元素,經過 TemplateRef 實例,咱們能夠方便建立內嵌視圖(Embedded Views),且能夠輕鬆地訪問到經過 ElementRef 封裝後的 nativeElement。須要注意的是組件視圖中的 template 模板元素,通過渲染後會被替換成 comment 元素。
ViewContainerRef:用於表示一個視圖容器,可添加一個或多個視圖。通ViewContainerRef 實例,咱們能夠基於 TemplateRef 實例建立內嵌視圖,並能指定內嵌視圖的插入位置,也能夠方便對視圖容器中已有的視圖進行管理。簡而言之,ViewContainerRef 的主要做用是建立和管理內嵌視圖或組件視圖。
詳細內容請參考 - Angular 2 TemplateRef & ViewContainerRef