Working with Angular 9 / typescript and looking to disable specific form elements based on a certain condition. I came across a helpful example where all elements are disabled, but noticed that MAT-SELECT remains active. How can I disable it?
https://i.sstatic.net/j9Khp.jpg
The original example code:
private disableElement(element: any) {
if (this.appDisable) {
if (!element.hasAttribute(DISABLED)) {
this.renderer.setAttribute(element, APP_DISABLED, '');
this.renderer.setAttribute(element, DISABLED, 'true');
// disabling anchor tab keyboard event
if (element.tagName.toLowerCase() === TAG_ANCHOR) {
this.renderer.setAttribute(element, TAB_INDEX, '-1');
}
}
} else {
if (element.hasAttribute(APP_DISABLED)) {
if (element.getAttribute('disabled') !== '') {
element.removeAttribute(DISABLED);
}
element.removeAttribute(APP_DISABLED);
if (element.tagName.toLowerCase() === TAG_ANCHOR) {
element.removeAttribute(TAB_INDEX);
}
}
}
if (element.children) {
for (let ele of element.children) {
this.disableElement(ele);
}
}
}
My modified code snippet:
private disableElement(element: any) {
if (this.appDisable) {
if (element.tagName == "INPUT" || element.tagName == "MAT-SELECT" || element.tagName == "BUTTON") {
if (!element.hasAttribute(DISABLED)) {
this.renderer.setAttribute(element, APP_DISABLED, '');
this.renderer.setAttribute(element, DISABLED, 'true');
// disabling anchor tab keyboard event
if (element.tagName.toLowerCase() === TAG_ANCHOR) {
this.renderer.setAttribute(element, TAB_INDEX, '-1');
}
}
}
} else {
if (element.tagName == "INPUT" || element.tagName == "MAT-SELECT" || element.tagName == "BUTTON") {
if (element.hasAttribute(APP_DISABLED)) {
if (element.getAttribute('disabled') !== '') {
element.removeAttribute(DISABLED);
}
element.removeAttribute(APP_DISABLED);
if (element.tagName.toLowerCase() === TAG_ANCHOR) {
element.removeAttribute(TAB_INDEX);
}
}
}
}
if (element.children) {
for (let ele of element.children) {
this.disableElement(ele);
}
}
}