Created
October 6, 2019 20:36
-
-
Save AvocadoVenom/ba3d09a916ecc5cb4241504668150d23 to your computer and use it in GitHub Desktop.
Overlay Loading Drective > Directive using injector
This file contains hidden or 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
| import { Directive, ElementRef, OnInit, Input, Injector } from '@angular/core'; | |
| import { OverlayRef } from '@angular/cdk/overlay'; | |
| import { Observable } from 'rxjs'; | |
| import { ComponentPortal, PortalInjector } from '@angular/cdk/portal'; | |
| import { DynamicOverlay } from './dynamic-overlay'; | |
| import { LoaderComponent } from './loader/loader.component'; | |
| import { LoaderData } from './loader/loader.config'; | |
| @Directive({ | |
| selector: '[overlayLoading]' | |
| }) | |
| export class OverlayLoadingDirective implements OnInit { | |
| @Input('overlayLoading') toggler: Observable<boolean>; | |
| @Input() loaderData: LoaderData; | |
| private overlayRef: OverlayRef; | |
| constructor( | |
| private host: ElementRef, | |
| private dynamicOverlay: DynamicOverlay, | |
| private parentInjector: Injector | |
| ) {} | |
| ngOnInit() { | |
| this.overlayRef = this.dynamicOverlay.createWithDefaultConfig( | |
| this.host.nativeElement | |
| ); | |
| this.toggler.subscribe(show => { | |
| if (show) { | |
| const injector = this.getInjector(this.loaderData, this.parentInjector); | |
| const loaderPortal = new ComponentPortal( | |
| LoaderComponent, | |
| null, | |
| injector | |
| ); | |
| this.overlayRef.attach(loaderPortal); | |
| } else { | |
| this.overlayRef.detach(); | |
| } | |
| }); | |
| } | |
| getInjector(data: LoaderData, parentInjector: Injector): PortalInjector { | |
| const tokens = new WeakMap(); | |
| tokens.set(LoaderData, data); | |
| return new PortalInjector(parentInjector, tokens); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment