简体   繁体   English

如何创建一个 Angular 指令来禁用输入字段

[英]How to create an Angular directive to disable input fields

I am trying to create a directive to be applied to inputs and buttons, that will disable the element based on a condition that I get from a service.我正在尝试创建一个应用于输入和按钮的指令,它将根据我从服务获得的条件禁用元素。

I have already reviewed, and tried the following:我已经审查过,并尝试了以下方法:

  • Button @Directive to bind disable attribute via @Input in Angular 按钮@Directive 通过@Input 在Angular 中绑定禁用属性
  • Angular2 attribute directive Hostbinding to host attribute Angular2 属性指令主机绑定到主机属性
  • I have tried using just disabled for the HostBinding, but I get Can't bind to 'disabled' since it isn't a known property of 'button'.我曾尝试仅对 HostBinding 使用disabled ,但我Can't bind to 'disabled' since it isn't a known property of 'button'.
  • I have tried using attr.disabled , but then I get Attempted to set attribute `disabled` on a container node. Host bindings are not valid on ng-container or ng-template.我曾尝试使用attr.disabled ,但后来我Attempted to set attribute `disabled` on a container node. Host bindings are not valid on ng-container or ng-template. Attempted to set attribute `disabled` on a container node. Host bindings are not valid on ng-container or ng-template.
  • I am sure that I am not trying to bind to ng-container or ng-template, I am binding directly to a button or input as in the example below我确定我不是要绑定到 ng-container 或 ng-template,我是直接绑定到按钮或输入,如下例所示
  • I am able to bind directly to disabled using [disabled]="condition" , but that is not what I want here我可以使用[disabled]="condition"直接绑定到 disabled ,但这不是我想要的

Here is what I currently have for the directive:这是我目前对该指令的了解:

export class InputPermissionsDirective {
  @Input() set appCanModify(name:string) {
    this.canModify(name);
  }
  @HostBinding('disabled') disabled:boolean = false;

  constructor(private permissionsService:PermissionsService) { }

  private canModify(name:string) {
    const routePermissions = this.permissionsService.getPermissionsByRoute(window.location.pathname);

    if(!routePermissions) this.disabled = true;
    else {
      const componentPermissions = this.permissionsService.getPermissionsByComponentName(routePermissions, name)

      if(componentPermissions && componentPermissions.CanModify) this.disabled = false;
      else this.disabled = true;
    }
  }

}

Here, the permissions service queries for the permission levels associated with the input name (FooBar in the below example).在这里,权限服务查询与输入名称关联的权限级别(下面示例中的 FooBar)。 From there, the disabled attribute should be set according to the element's permission field CanModify.从那里开始,应根据元素的权限字段 CanModify 设置禁用属性。

And here is an example of me using this directive:这是我使用此指令的示例:

<input type="text" class="form-control" [(ngModel)]="foo" *appCanModify="'FooBar'">

Any help here would be greatly appreciated, and I can provide additional info as needed.非常感谢这里的任何帮助,我可以根据需要提供其他信息。 Thanks!谢谢!

The solution to my issue was that I was trying to use this directive as a structural directive by using the asterisk.我的问题的解决方案是我试图通过使用星号将此指令用作结构指令。 Instead, I needed to use it as appCanModify相反,我需要将它用作appCanModify

Thanks to @MikkelChristensen for the solution感谢@MikkelChristensen 提供的解决方案

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM