-
-
Notifications
You must be signed in to change notification settings - Fork 130
/
link.directive.ts
35 lines (29 loc) · 842 Bytes
/
link.directive.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
import { Directive, ElementRef, OnDestroy, OnInit, Optional } from '@angular/core';
import { RouterLink, RouterLinkWithHref } from '@angular/router';
import { LinkHandlerService } from './link-handler.service';
@Directive({
selector: '[routerLink]',
})
export class LinkDirective implements OnInit, OnDestroy {
private routerLink: RouterLink | RouterLinkWithHref;
constructor(
private linkHandler: LinkHandlerService,
private el: ElementRef,
@Optional() link: RouterLink,
@Optional() linkWithHref: RouterLinkWithHref,
) {
this.routerLink = link || linkWithHref;
}
ngOnInit() {
this.linkHandler.register(this);
}
ngOnDestroy() {
this.linkHandler.unregister(this);
}
get element(): Element {
return this.el.nativeElement;
}
get urlTree() {
return this.routerLink.urlTree;
}
}