has-permission.directive.ts
angular/examples/week07-08/src/app/has-permission.directive.ts
// @ts-nocheck
import { Directive, Input, TemplateRef, ViewContainerRef } from '@angular/core';
import { AuthHttpService } from './auth-http.service';
@Directive({
selector: '[appHasPermission]',
standalone: true,
})
export class HasPermissionDirective {
private hasView = false;
constructor(
private templateRef: TemplateRef<unknown>,
private viewContainer: ViewContainerRef,
private auth: AuthHttpService,
) {}
@Input()
set appHasPermission(role: string) {
const allowed = this.auth.hasRole(role);
if (allowed && !this.hasView) {
this.viewContainer.createEmbeddedView(this.templateRef);
this.hasView = true;
} else if (!allowed && 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).
글 읽기 →