-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
45 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,37 @@ | ||
import { Directive, Input, ViewContainerRef, TemplateRef } from '@angular/core'; | ||
import { Directive, Input, ViewContainerRef, TemplateRef, OnDestroy } from '@angular/core'; | ||
|
||
@Directive({ selector: '[timeout]' }) | ||
export class TimeoutDirective { | ||
export class TimeoutDirective implements OnDestroy { | ||
|
||
private timeoutId: number; | ||
|
||
constructor( | ||
private templateRef: TemplateRef<null>, | ||
private viewContainerRef: ViewContainerRef | ||
) { | ||
} | ||
) {} | ||
|
||
@Input() set timeout(milliseconds: number) { | ||
this.viewContainerRef.clear(); | ||
this.dispose(); | ||
this.create(milliseconds); | ||
} | ||
|
||
setTimeout(() => { | ||
this.viewContainerRef.createEmbeddedView(this.templateRef); | ||
}, milliseconds); | ||
ngOnDestroy() { | ||
this.dispose(); | ||
} | ||
|
||
private create(milliseconds: number) { | ||
this.timeoutId = setTimeout(() => { | ||
if (this.viewContainerRef) { | ||
this.viewContainerRef.createEmbeddedView(this.templateRef); | ||
} | ||
}, milliseconds) as any as number; | ||
} | ||
|
||
private dispose() { | ||
this.viewContainerRef.clear(); | ||
if (this.timeoutId) { | ||
clearTimeout(this.timeoutId); | ||
this.timeoutId = null; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters