Skip to content

Instantly share code, notes, and snippets.

@fxck
Created October 5, 2016 17:44
Show Gist options
  • Save fxck/b668f7fec77d7b28d8c7ce6b706601f7 to your computer and use it in GitHub Desktop.
Save fxck/b668f7fec77d7b28d8c7ce6b706601f7 to your computer and use it in GitHub Desktop.
import {
Component,
Directive,
TemplateRef,
ViewContainerRef,
ViewChild,
Input,
OnDestroy,
AfterViewInit
} from '@angular/core';
import {
TemplatePortal,
OverlayState,
OverlayRef,
Overlay
} from '@angular2-material/core';
@Directive({ selector: '[displacerPortal]' })
export class DisplacerPortalDirective extends TemplatePortal {
constructor(templateRef: TemplateRef<any>, viewContainerRef: ViewContainerRef) {
super(templateRef, viewContainerRef);
}
}
@Component({
selector: 'fnls-displacer',
template: `
<template displacerPortal>
<ng-content></ng-content>
</template>
`
})
export class DisplacerComponent implements OnDestroy, AfterViewInit {
private _config = new OverlayState();
@ViewChild(DisplacerPortalDirective)
private _portal: DisplacerPortalDirective;
private _overlayRef: OverlayRef = undefined;
constructor(private _overlay: Overlay) {}
public ngOnDestroy() {
this._overlayRef.detach();
}
public ngAfterViewInit() {
this._overlayRef = this._overlay.create(this._config);
this._overlayRef.attach(this._portal);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment