【发布时间】:2018-11-16 04:00:45
【问题描述】:
我通过控制访问使用属性来检查是否应该启用或禁用字段。
所以我有这样的东西:
<input matInput type="text" #name="ngModel" name="application.name" [ngModel]="application?.name" (ngModelChange)="application.name=$event"
placeholder="Nom de l'application" required [disabled]="isDisabled([permissions.applications.any.update, permissions.applications.own.update], 'name')"[appUnique]="application" [listElem]="applications" key="name" maxlength="255">
当显示输入时,isDisabled 函数会被调用数百次,并且表单上的每个操作都会触发 12 或 32 次额外调用。
这会导致我的浏览器死机。
知道为什么会发生这种行为吗?是 ZoneJS 造成的吗?
如何只调用这个函数?
编辑:指令实现
/**
* Check if the component should be disabled
*/
@Directive({
selector: "[appIsDisabled]"
})
export class IsDisabledDirective implements OnInit {
// Listof permission to check
@Input("appIsDisabled") permissions: PermissionInterface[];
// The resource id to be access
@Input() resourceId: number;
// The resource type to be access
@Input() resourceType: string;
// Attribute to be check(eg: name, firstname... for a user resource)
@Input() attribute: string;
constructor(private authService: AuthService, private el: ElementRef, private utilsService: UtilsService, private renderer: Renderer2) {
}
ngOnInit() {
if (!this.authService.hasPermission(this.permissions, this.resourceId, this.resourceType, this.attribute)) {
this.renderer.setAttribute(this.el.nativeElement, "disabled", "disabled");
}
}
}
【问题讨论】:
-
有一些关于这种行为的好文章:blog.appverse.io/…
-
好的,我知道角循环,但不知道它也适用于属性。文本文件。我会找到另一种解决方案来控制输入的禁用状态。如果有人提出建议,那就太好了
标签: javascript angular forms typescript disabled-input