Last active
February 9, 2021 09:27
-
-
Save NetanelBasal/a6008c6cbf4d90e5a11e80286d3e1dd3 to your computer and use it in GitHub Desktop.
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
import { untilDestoryed, UntilDestroy } from '@ngneat/until-destroy'; | |
const breakpoints = { ... }; | |
@UntilDestroy() | |
@Directive({ | |
selector: '[matches]' | |
}) | |
export class MatchesDirective implements OnInit { | |
@Input() matches: keyof typeof breakpoints; | |
constructor(private tpl: TemplateRef<any>, | |
private breakpointObserver: BreakpointObserver, | |
private vcr: ViewContainerRef) { | |
} | |
ngOnInit() { | |
const breakpoint = breakpoints[this.matches]; | |
fromMediaQuery(breakpoint).pipe(untilDestroyed(this)).subscribe(matches => { | |
matches ? this.vcr.createEmbeddedView(this.tpl) : this.vcr.clear(); | |
}); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Very nice solution, indeed.
I guess
untilDestoryed
should beuntilDestroyed
:-)