【问题标题】:Setting disabled to angular material components using directive使用指令将禁用设置为角度材​​料组件
【发布时间】:2020-07-28 03:56:18
【问题描述】:

正如标题所说,我正在尝试使用指令将禁用设置为材质组件。我尝试了各种方法,使用 ElementRef、Renderer、Renderer2 和 querySelector。似乎没有任何工作。

这是我的代码。任何帮助表示赞赏。

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();
    }
  }

}

例如,我试图禁用此按钮图标。

<button mat-icon-button class="action--icon" matTooltip="Notes" matTooltipPosition="above" (click)="openNotesDialog(element.earningsFileId)" *appIsGranted="['EARNINGS', 'viewearnings']">
  <mat-icon>chat</mat-icon>
</button>

我们的想法是,只需将属性 disabled 添加到所有材质组件,这将适用于所有材质项目。

【问题讨论】:

  • 与其为禁用添加指令,不如为该元素使用属性绑定,如 [disabled]="yourCondition()" ?
  • @Indrajeet - 如果您使用指令,您可以将其应用于任何元素。见angular.io/guide/attribute-directives。这是为 DOM 元素提供可重用自定义行为的好方法。
  • 是的,但通常禁用对每个元素都可用
  • 只要您为禁用创建指令,就可以避免。如果用例不同,例如添加星号使其看起来像必需的,那么使用指令很有意义。你不觉得吗?
  • @Indrajeet disabled 属性并非对所有元素都可用。检查这个:w3schools.com/tags/att_disabled.asp

标签: javascript angular


【解决方案1】:

我们正在我们的应用程序中通过一个指令执行类似的操作,该指令传递了运行与其设置的元素相关联的操作所需的角色列表。

它通过使用JQuery 的方法attr 来设置disabled 和其他属性来工作。

指令;

@Directive({
    selector: '[appEditEntityActions]'
})
export class EditEntityActionsDirective implements OnInit {
    @Input() requiresAnyRole: string[] = null;

    constructor(
        private authorizationService: AuthorizationService,
        private element: ElementRef) { }

    ngOnInit() {
        var userCanEdit = this.authorizationService.hasAnyClaim(this.requiresAnyRole);
        if (!userCanEdit) {
            this.turnOffElement();
        }
    }

    private turnOffElement(): void {
        var jqElem = $(this.element.nativeElement);

        jqElem.off();
        jqElem.find('*').off();

        // app-opac-50-disabled-cursor-not-authorized is a css class that sets cursor, transparency etc...
        jqElem
            .attr('disabled', 'disabled')
            .attr('href', '')
            .addClass('app-opac-50-disabled-cursor-not-authorized')
            .attr('title', 'Not authorized');

        jqElem
            .find('button')
            .addClass('text-muted')
            .attr('disabled', 'disabled');

        jqElem.find('a')
            .attr('href', '')
            .addClass('app-opac-50-disabled-cursor-not-authorized');

        jqElem.on('click', (e: Event) => {
            e.preventDefault();
        });
    }
}

用法

<button appEditEntityActions [requiresAnyRole]='["Viewer", "Editor"]' (click)="doSomething();">Sample button</button>

希望对你有帮助。

【讨论】:

猜你喜欢
  • 1970-01-01
  • 2019-04-18
  • 1970-01-01
  • 2018-05-09
  • 2019-01-07
  • 1970-01-01
  • 2019-03-18
  • 2020-02-22
  • 1970-01-01
相关资源
最近更新 更多