As the title says I am trying to set disabled to material component using a directive. I've tried various ways, using ElementRef, Renderer, Renderer2 and querySelector. Nothing seems to be working.
Here it my code. Any help is appreciated.
import { Directive, Input, TemplateRef, ViewContainerRef, Renderer2, ElementRef } from '@angular/core';
import { PermissionType } from './permission-type.enum';
import { Resource } from './resource.enum';
import { PermissionManagerService } from './permission-manager.service';
@Directive({
selector: '[appIsGranted]'
})
export class IsGrantedDirective {
constructor(
private templateRef: TemplateRef<any>,
private viewContainer: ViewContainerRef,
private permissionManagerS: PermissionManagerService,
private _renderer: Renderer2,
private el: ElementRef
) { }
@Input() set appIsGranted(permission: Array<string>) {
this.isGranted(
permission[0] as Resource,
permission[1] as PermissionType
)
}
private isGranted(resource: Resource, permissionType: PermissionType) {
if(this.permissionManagerS.isGranted(resource, permissionType)) {
this.viewContainer.createEmbeddedView(this.templateRef);
} else {
let view = this.viewContainer.createEmbeddedView(this.templateRef);
let rootElem = view.rootNodes[0];
//this.el.nativeElement.disabled = true;
//this.el.nativeElement.disabled = 'disabled';
//this._renderer.setProperty(rootElem, 'disabled', true);
this._renderer.setProperty(rootElem, 'disabled', 'disabled');
// this.viewContainer.clear();
}
}
}
For example this button icon is what I am trying to disable.
<button mat-icon-button class="action--icon" matTooltip="Notes" matTooltipPosition="above" (click)="openNotesDialog(element.earningsFileId)" *appIsGranted="['EARNINGS', 'viewearnings']">
<mat-icon>chat</mat-icon>
</button>
The idea would be that this would work across all material items by just adding the attribute disabled to all material components.