在 Angular 中有三种类型的指令:
组件,有模板的指令,组件是继承于指令的,只是扩展类与 UI 相关的属性。
属性型指令,改变 DOM 元素、组件或其他指令的行为和外观的指令。如,NgStyle、NgClass。
结构型指令,通过添加或移除 DOM 元素改变 DOM 布局的指令。如,NgIf、NgFor。
然而,在实际的开发中,根据业务的需要,我们经常会拓展 Angular 组件的指令来方便业务的开发。下面让我们来看看如何创建自己的指令。
创建属性型指令
在 Angular 中,属性型指令的创建至少需要一个带有 @Directive
装饰器的控制器类。这个装饰器指定了一个选择器名称,用于标识与指令相关联的属性名称。控制器类实现了指令的功能行为。
接下来,我们创建一个简单的指令,实现鼠标在元素上悬停时,改变起背景颜色;鼠标移开时,背景颜色消失;鼠标点击时,字体变大;鼠标松开时,字体恢复原样的功能。
指令实现
创建 background-exed.directive.ts 文件,实现如下代码:
1 | import { Directive, HostListener, ElementRef, Renderer2, HostBinding } from '@angular/core'; |
2 | |
3 | ({ |
4 | selector: '[appBackgroundExe]' |
5 | }) |
6 | export class BackgroundExeDirective { |
7 | |
8 | 'appBackgroundExe') ( |
9 | highLightColor: string; |
10 | |
11 | constructor(private elementRef: ElementRef, private renderer: Renderer2) { |
12 | // 这种写法比较丑陋 |
13 | // this.elementRef.nativeElement.style.background = 'yellow'; |
14 | // 推荐这种写法, Renderer |
15 | this.renderer.setStyle(this.elementRef.nativeElement, 'background', 'yellow'); |
16 | } |
17 | |
18 | 'class.pressed') ( |
19 | isPressed: boolean; |
20 | |
21 | 'mouseenter') ( |
22 | onMouseEnter(): void { |
23 | this.highLight(this.highLightColor); |
24 | } |
25 | |
26 | 'mouseleave') ( |
27 | onMouseLeave(): void { |
28 | this.highLight(null); |
29 | } |
30 | |
31 | 'mousedown') ( |
32 | onMouseDown(): void { |
33 | this.isPressed = true; |
34 | } |
35 | |
36 | 'mouseup') ( |
37 | onMouseUp(): void { |
38 | this.isPressed = false; |
39 | } |
40 | |
41 | private highLight(color: string): void { |
42 | // this.elementRef.nativeElement.style.background = color; |
43 | this.renderer.setStyle(this.elementRef.nativeElement, 'background', color); |
44 | } |
45 | |
46 | } |
其中,selector: '[appBackgroundExe]'
是指令关联的属性名称,以便 Angular 在编译时,能从模板中找到与此指令关联的 HTML 代码。
构造函数中,注入了 ElementRef
和 Renderer2
模块的实例。通过 ElementRef
我们可以引用指令标识的 DOM 元素,并对其进行相关的操作;并且可以利用 Renderer2
提供的 API 对元素进行相关的渲染操作。
@HostListener
和 @HostBinding
是属性装饰器。@HostListener
是用来为宿主元素添加事件监听;而指令标记的元素,就是宿主元素。@HostBinding
是用来动态设置宿主元素的属性值。
设置字体样式
- appliation.component.less
1 | .pressed { |
2 | font-size: 30px; |
3 | } |
在模板中使用指令
- application.component.html
1 | <div class="panel panel-primary"> |
2 | <div [appBackgroundExe]="'red'">鼠标移进,元素变成红色。鼠标移出,元素红色消失</div> |
3 | </div> |
创建结构型指令
结构型指令的创建与属性型指令创建大同小异。
指令实现
1 | import { Directive, Input, TemplateRef, ViewContainerRef } from '@angular/core'; |
2 | |
3 | ({ |
4 | selector: '[appIf]' |
5 | }) |
6 | export class IfDirective { |
7 | |
8 | constructor( |
9 | private templateRef: TemplateRef<any>, |
10 | private viewContainerRef: ViewContainerRef |
11 | ) { } |
12 | |
13 | 'ifCreat') ( |
14 | set condition(condition: boolean) { |
15 | if (condition) { |
16 | this.viewContainerRef.createEmbeddedView(this.templateRef); |
17 | } else { |
18 | this.viewContainerRef.clear(); |
19 | } |
20 | } |
21 | } |
其中,TemplateRef
表示内嵌的 template 模板元素,通过它可以创建内嵌视图。ViewContainerRef
表示一个视图容器,可以添加一个或多个视图,通过它可以创建和管理基于 TemplateRef
实例的内嵌视图或组件视图。
在模板中使用指令
- application.component.html
1 | <div class="panel panel-primary"> |
2 | <div *ifCreate="'true'">hello</div> |
3 | </div> |
小结
本文主要介绍了在 Angular 中如何自定义创建指令。在实际的开发中,我们可以很灵活地创建我们想要的指令。