angular指令,目的在于影响Dom布局,或者修改Dom属性。css
an extension of @Directive()html
import { Component, OnInit } from '@angular/core';
@Component({ selector: 'idata', templateUrl: './user.component.html', styleUrls: ['./user.component.scss'] }) export class UserComponent implements OnInit { constructor() { } ngOnInit(): void { } } 复制代码
export declare interface Component extends Directive {
changeDetection?: ChangeDetectionStrategy; viewProviders?: Provider[]; moduleId?: string; templateUrl?: string; template?: string; styleUrls?: string[]; styles?: string[]; animations?: any[]; encapsulation?: ViewEncapsulation; interpolation?: [string, string]; entryComponents?: Array<Type<any> | any[]>; preserveWhitespaces?: boolean; } 复制代码
从中咱们得出以下:web
export declare interface Directive {
selector?: string; inputs?: string[]; outputs?: string[]; providers?: Provider[]; exportAs?: string; queries?: { [key: string]: any; }; host?: { [key: string]: string; }; jit?: true; } 复制代码
Attribute directives are used as attributes of elementsbootstrap
# 要求不能ng开头
ng generate directive highlight 复制代码
# src/app/highlight.directive.ts
import { Directive } from '@angular/core'; @Directive({ selector: '[appHighlight]' }) export class HighlightDirective { constructor() { } } 复制代码
import { Directive, ElementRef } from '@angular/core';
@Directive({ selector: '[appHighlight]' }) export class HighlightDirective { constructor(el: ElementRef) { # 修改元素背景 el.nativeElement.style.backgroundColor = 'yellow'; } } 复制代码
import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core'; import { AppRoutingModule } from './app-routing.module'; import { AppComponent } from './app.component'; import { HighlightDirective } from './highlight.directive'; @NgModule({ declarations: [ AppComponent, HighlightDirective # 视图中声明指令的引用 ], imports: [ BrowserModule, AppRoutingModule, ], providers: [], bootstrap: [AppComponent], exports: [] }) export class AppModule { } 复制代码
<p appHighlight>Highlight me!</p>
复制代码
Structural directives are responsible for HTML layoutsegmentfault
# false,不渲染元素,而非渲染后隐藏
<div *ngIf="hero" class="name">{{hero.name}}</div> 复制代码
<ng-template [ngIf]="hero">
<div class="name">{{hero.name}}</div> </ng-template> 复制代码
<div *ngFor="let hero of heroes; let i=index; let odd=odd; trackBy: trackById" [class.odd]="odd">
({{i}}) {{hero.name}} </div> 复制代码
<ng-template ngFor let-hero [ngForOf]="heroes" let-i="index" let-odd="odd" [ngForTrackBy]="trackById">
<div [class.odd]="odd">({{i}}) {{hero.name}}</div> </ng-template> 复制代码
<div [ngSwitch]="hero?.emotion">
<app-happy-hero *ngSwitchCase="'happy'" [hero]="hero"></app-happy-hero> <app-sad-hero *ngSwitchCase="'sad'" [hero]="hero"></app-sad-hero> <app-confused-hero *ngSwitchCase="'confused'" [hero]="hero"></app-confused-hero> <app-unknown-hero *ngSwitchDefault [hero]="hero"></app-unknown-hero> </div> 复制代码
ng generate directive appUnless
复制代码
import { Directive } from '@angular/core';
@Directive({ selector: '[appUnless]' }) export class AppUnlessDirective { constructor() { } } 复制代码
import { Directive, TemplateRef, ViewContainerRef, Input } from '@angular/core';
@Directive({ selector: '[appUnless]' }) export class AppUnlessDirective { private hasView = false; constructor( private templateRef: TemplateRef<any>, private viewContainer: ViewContainerRef) { } @Input() set appUnless(condition: boolean) { if (!condition && !this.hasView) { this.viewContainer.createEmbeddedView(this.templateRef); this.hasView = true; } else if (condition && this.hasView) { this.viewContainer.clear(); this.hasView = false; } } } 复制代码
import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core'; import { AppRoutingModule } from './app-routing.module'; import { AppComponent } from './app.component'; import { HighlightDirective } from './highlight.directive'; import { AppUnlessDirective } from './app-unless.directive'; @NgModule({ declarations: [ AppComponent, HighlightDirective, AppUnlessDirective # 声明结构指令 ], imports: [ BrowserModule, AppRoutingModule, ], providers: [], bootstrap: [AppComponent], exports: [] }) export class AppModule { } 复制代码
<p *appUnless="condition" class="unless a">
(A) This paragraph is displayed because the condition is false. </p> #ts public condition = false; constructor(private domSanitizer: DomSanitizer){ interval(2000).subscribe(() => { this.condition = !this.condition; }); } 复制代码