Я использую атрибут с контролем доступа, чтобы проверить, должно ли поле быть включено или отключено.
Итак, у меня есть что-то вроде этого:
<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");
}
}
}