我正在尝试创建一个应用于输入和按钮的指令,该指令将根据从服务获得的条件禁用元素。
我已经进行了审查,并尝试了以下几点:
disabled
,但我得到了Can't bind to 'disabled' since it isn't a known property of 'button'.
attr.disabled
,但是我得到Attempted to set attribute `disabled` on a container node. Host bindings are not valid on ng-container or ng-template.
[disabled]="condition"
直接绑定到禁用的位置,但这不是我想要的。
以下是我目前对该指令的看法:
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;
}
}
}
在这里,权限服务查询与输入名称关联的权限级别(在下面的示例中是FooBar)。在那里,应该根据元素的权限字段CanModify设置禁用的属性。
下面是我使用这个指令的一个例子:
<input type="text" class="form-control" [(ngModel)]="foo" *appCanModify="'FooBar'">
任何帮助在这里将非常感谢,我可以提供更多的信息,视需要。谢谢!
发布于 2021-11-15 06:07:44
解决我的问题的办法是,我试图使用星号将这个指令作为结构指令使用。相反,我需要使用它作为appCanModify
感谢@MikkelChristensen的解决方案
https://stackoverflow.com/questions/69949755
复制相似问题