Last active
December 31, 2020 06:49
-
-
Save Bilkiss/9b398b0d3f0b8d8128f90099f4f60951 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 {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