I am new to Angular.
I have a following button control in html file and i am trying to enable it in the ts (type script) file on map click event.
<div *ngIf="editVisible" class="p-col-1">
<button label="Update" icon="far fa-save" class="p-button-raised p-button-primary" pButton
[disabled]="!has(Permission.Admin_Pod_Location_Full_Control) || cleanData"
(click)="updateLocation()"></button>
</div>
I have tried this in the Type Script file button it doesn’t enable the button.
MapClick(event) {
const button = document.getElementsByClassName('p-button-raised p-button-primary');
button.setAttribute('disabled', '');
}
2
Answers
getElementsByClassName
method returns an array-like collection of elements, not a single element.you need to access the specific button you want to disable using an index.
Try this:
(Assuming that you want to disable the first button in the collection.)
To enable the button in your TypeScript file, you should use Angular’s data binding and property binding approach rather than directly manipulating the DOM using
document.getElementsByClassName
. Here’s how you can achieve this:component.ts
), create a boolean property to track the state of the button:component.html
), bind thedisabled
property of the button to theeditVisible
property defined in the TypeScript file:With this approach, when the
editVisible
property is set totrue
, the button will be enabled, and when it’s set tofalse
, the button will be disabled automatically based on the data binding. You can then modify theMapClick
method to toggle the value ofeditVisible
based on your specific conditions and requirements. Avoid direct DOM manipulation whenever possible in Angular, as it is against the framework’s principles and can lead to unexpected behavior.