指令是为 Angular 应用程序中的元素添加额外行为的类。使用 Angular 的内置指令,你可以管理表单、列表、样式以及要让用户看到的任何内容。
Angular 指令的不同类型如下:
内置属性指令: ngClass, ngStyle,ngModel
内置结构指令: ngIf, ngFor, ngSwitch(自定义结构性指令还是很难得)
[ngIf] 可以用微语法糖简化, *ngIf
import { Directive, ElementRef, HostListener, Input } from '@angular/core';
@Directive({
selector: '[appHighlight]'
})
export class HighlightDirective {
@Input() appHighlight:string = '';
@Input() defaultColor: string = '';
constructor(private el: ElementRef) {
// console.log(el);
// el.nativeElement.style.backgroundColor = "yellow"
}
@HostListener('mouseenter') onMouseEnter() {
this.highLight(this.appHighlight || this.defaultColor || 'red');
}
@HostListener('mouseleave') onMouseLeave() {
this.highLight('');
}
private highLight(color: string) {
this.el.nativeElement.style.backgroundColor = color
}
}
<!-- <p appHighlight>Highlight me!</p> -->
<div>
<input type="radio" name="colors" (click)="color='lightgreen'" /> Green
<input type="radio" name="colors" (click)="color='yellow'" /> Yellows
<input type="radio" name="colors" (click)="color='cyan'" /> Cyan
</div>
<!--ngNonBindable停止插值表达式计算-->
<p ngNonBindable [appHighlight]="color">Highlight me! {{1+1}}</p>
<!--没有加中括号,静态属性-->
<p [appHighlight]="color" defaultColor="violet">Highlight me, too!</p>
import { Directive, Input, ElementRef, SecurityContext } from '@angular/core';
import { DomSanitizer } from '@angular/platform-browser';
@Directive({
selector: '[appTextHighlight]'
})
export class TextHighlightDirective {
@Input() set highlightColor(color: string) {
this.color = color;
this.updateHighlight();
}
@Input() set highlightText(hl: string) {
const esc = hl.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
if (esc) {
this.highlight = new RegExp(esc, 'ig');
} else {
this.highlight = null;
}
this.updateHighlight();
}
@Input() set appTextHighlight(text: string) {
this.text = this.sanitizer.sanitize(SecurityContext.HTML, text);
this.updateHighlight();
}
private color = '#ffd400';
private text: string;
private highlight: RegExp;
constructor(
private el: ElementRef<HTMLElement>,
private sanitizer: DomSanitizer
) {}
private updateHighlight() {
if (this.text && this.el && this.el.nativeElement) {
const elem = this.el.nativeElement;
elem.innerHTML = this.highlight
? this.text.replace(
this.highlight,
`${this.color}">$&`
)
: this.text;
} else {
console.warn('Cannot apply highlight to null or non-HTML objects');
}
}
}
import { Directive, TemplateRef, ViewContainerRef, Input } from '@angular/core';
@Directive({
selector: '[appUnless]'
})
export class UnlessDirective {
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;
}
}
}
<!-- <p *appUnless="condition">Show this sentence unless the condition is true.</p> -->
<p>
The condition is currently
<span [ngClass]="{'a': condition, 'b': !condition, 'unless': true}">{{condition}}</span>
<button (click)="condition=!condition" [ngClass]="{'a': !condition, 'b': condition}"> Toggle condition to {{condition ? 'false' : 'true'}}</button>
</p>
<p *appUnless="condition" class="unless a">
(A) This paragraph is displayed because the condition is false.
</p>
<p *appUnless="!condition" class="unless b">
(B) Although the condition is true,
this paragraph is displayed because appUnless is set to false.
</p>