Skip to content

Instantly share code, notes, and snippets.

@Bilkiss
Last active December 31, 2020 06:49
Show Gist options
  • Save Bilkiss/9b398b0d3f0b8d8128f90099f4f60951 to your computer and use it in GitHub Desktop.
Save Bilkiss/9b398b0d3f0b8d8128f90099f4f60951 to your computer and use it in GitHub Desktop.
import {Directive, Input, TemplateRef, ViewContainerRef} from '@angular/core';
@Directive({
selector: '[enableForRole]',
})
export class EnableForRoleDirective {
constructor(
private viewContainerRef: ViewContainerRef,
private templateRef: TemplateRef<any>
) {}
// the role the user must have
@Input() set enableForRole(role: string) {
let userObj = JSON.parse(localStorage.getItem('user'));
if (userObj.role == role)) {
// If user has role, show element
this.viewContainerRef.createEmbeddedView(this.templateRef);
} else {
// Else clear element
this.viewContainerRef.clear();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment