Set the disabled attribute to the result of an expression and Angular will disable / enable the element for you as needed.
Disable an HTML Element – Example # 1
The focus of this article is the Angular disabled attribute. Next, take a look at the third button. It has a disabled attribute. That attribute is set to toggle based on the buttonDisabled property. So when the buttonDisabled property is true, that third button is disabled. And when the buttonDisabled property is false, the third button is. A disabled input is of course also not editable, but it has a distinct appearance - greyed out and looks 'disabled'. A read-only input looks the same as a normal input, but is not editable. So you have to ask whether you want your controls actually disabled or just read-only.
2 4 6 8 10 | selector:'home', }) count:number=0; } |
In example # 1, we have our component. There are two properties we are interested in: count and buttonDisabled. We will leverage these in our template.
Mat Input Set Disabled Windows 10
Disable an HTML Element – Example # 2
2 4 6 8 10 12 14 | <button> </button> <!--enable the button--> Enable Button <button disabled='disabled'> </button> |
In example # 2, we have our template. There are three buttons. The first two buttons toggle each other. That is to say, when the first button is clicked it is destroyed, and the second button is created. Conversely, when the second button is clicked, it is destroyed and the first button is created. All of this happens because each button has an ngIf attribute. That attribute is watching for the value of the buttonDisabled property. Also, each button has a click attribute. In each case, clicking the button updates the value of the buttonDisabled property.
Important note: these two buttons are for demonstration purposes only. The focus of this article is the Angular [disabled] attribute.
Next, take a look at the third button. It has a disabled attribute. That attribute is set to toggle based on the buttonDisabled property. So when the buttonDisabled property is true, that third button is disabled. And when the buttonDisabled property is false, the third button is not disabled.
Video Example Code
Mat Input Set Disabled File
If you want to download the example code, visit this Github page, and then follow the instructions: bit.ly/kcv-angular-disabled-attribute
Comments are closed.