src/app/modules/core/directives/permission/permission.directive.ts
Permission validator Directive
OnInit
Selector | [appPermission] |
Properties |
|
Methods |
Inputs |
constructor(elementRef: ElementRef, permissionService: PermissionService)
|
|||||||||
constructor
Parameters :
|
permission | |
Type : Array<string>
|
|
Permission to validate |
ngOnInit |
ngOnInit()
|
Returns :
void
|
Public elementRef |
Type : ElementRef
|
reference of permissionService service. |
Public permissionService |
Type : PermissionService
|
reference of permissionService service. |
import { PermissionService } from './../../services';
import { Directive, ElementRef, Input, OnInit} from '@angular/core';
/**
* Permission validator Directive
*/
@Directive({
selector: '[appPermission]'
})
export class PermissionDirective implements OnInit {
/**
* Permission to validate
*/
@Input() permission: Array<string>;
/**
* reference of permissionService service.
*/
public permissionService: PermissionService;
/**
* reference of permissionService service.
*/
public elementRef: ElementRef;
/**
* constructor
*/
constructor(elementRef: ElementRef, permissionService: PermissionService) {
this.elementRef = elementRef;
this.permissionService = permissionService;
}
ngOnInit() {
this.permissionService.permissionAvailable$.subscribe(
(permissionAvailable: string) => {
if (permissionAvailable && permissionAvailable === 'success') {
if (!this.permissionService.checkRolesPermissions(this.permission)) {
this.elementRef.nativeElement.remove();
}
} else if (permissionAvailable && permissionAvailable === 'error') {
this.elementRef.nativeElement.remove();
}
});
}
}