Hi,
I have a button like below
How to call the id - btn from component to typescript file to work for enable or disable
Thanks
Hi,
I have a button like below
How to call the id - btn from component to typescript file to work for enable or disable
Thanks
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Tuhin PaulPosted Mar 16, 2023, 4:51 AM
For the code below:
there are a few style and best practice improvements that could be made:
1) It's generally a good practice to use a more descriptive name for the function, such as "toggleButtonState" or "enableDisableButton".
2) Since we know the type of the element we are accessing, we can use the more specific HTMLButtonElement type instead of casting to an HTMLButtonElement using the "as" keyword.
3) It's a good practice to cache the result of getElementById to avoid searching the DOM tree multiple times for the same element.
4) If we're just toggling the button state, we can use the "toggle" method instead of setting the "disabled" property directly.
we use the querySelector method to select the button element by its ID, and use the toggleAttribute method to toggle the "disabled" attribute of the button.
Jignesh KumarPosted Mar 16, 2023, 5:14 AM
Hi satish,
Please refer this link which will help you,
https://stackblitz.com/edit/angular-7pdxqk?file=src%2Fapp%2Fapp.component.html
Tuhin PaulPosted Mar 16, 2023, 4:34 AM
In your component class, import the ViewChild decorator from @angular/core and use it to get a reference to the button element:
we use @ViewChild to get a reference to the button element using its template reference variable "#btn". We then store the reference to the button element in a class property called "btnElement".
Naimish MakwanaPosted Mar 16, 2023, 3:51 AM
Hello Sathish,
Try below code:
Thanks
Naimish Makwana
Vishal YelvePosted Mar 15, 2023, 6:52 PM
Please refer below link
https://bobbyhadz.com/blog/typescript-disable-button
https://tutorialscamp.com/angular-disable-button/
Tuhin PaulPosted Mar 15, 2023, 3:23 PM
To access the
btnbutton from the component's TypeScript file, you can use Angular'sViewChilddecorator to get a reference to the button element.