if-viewport.directive.ts
angular/examples/week03-04/src/app/shared/if-viewport.directive.ts
// @ts-nocheck
import { Directive, Input, TemplateRef, ViewContainerRef } from '@angular/core';
@Directive({
selector: '[appIfViewport]',
standalone: true,
})
export class IfViewportDirective {
private hasView = false;
private minWidth = 768;
constructor(private templateRef: TemplateRef<unknown>, private viewContainer: ViewContainerRef) {}
@Input()
set appIfViewport(minWidth: number) {
this.minWidth = minWidth || 768;
this.updateView();
}
ngOnInit(): void {
this.updateView();
window.addEventListener('resize', this.updateView);
}
ngOnDestroy(): void {
window.removeEventListener('resize', this.updateView);
}
private updateView = () => {
const shouldShow = window.innerWidth >= this.minWidth;
if (shouldShow && !this.hasView) {
this.viewContainer.createEmbeddedView(this.templateRef);
this.hasView = true;
} else if (!shouldShow && this.hasView) {
this.viewContainer.clear();
this.hasView = false;
}
};
}
관련 글
server.js
server.js — javascript source code from the angular learning materials (angular/comprehensive-demo/server/server.js).
글 읽기 →app.component.ts
app.component.ts — typescript source code from the angular learning materials (angular/comprehensive-demo/src/app/app.component.ts).
글 읽기 →app.config.ts
app.config.ts — typescript source code from the angular learning materials (angular/comprehensive-demo/src/app/app.config.ts).
글 읽기 →app.routes.ts
app.routes.ts — typescript source code from the angular learning materials (angular/comprehensive-demo/src/app/app.routes.ts).
글 읽기 →auth.guard.ts
auth.guard.ts — typescript source code from the angular learning materials (angular/comprehensive-demo/src/app/core/guards/auth.guard.ts).
글 읽기 →role.guard.ts
role.guard.ts — typescript source code from the angular learning materials (angular/comprehensive-demo/src/app/core/guards/role.guard.ts).
글 읽기 →