Binge On Code

Jun 19, 2023

46 readers

How To Disable Angular HTML Elements Programmatically

At times, we need to disable Angular HTML element like buttons, inputs or even material element, let’s see how we can do this in simple examples.

One of the most common HTML elements which we use for getting user feedback oftentimes need to be disabled. We will see how we can disable Angular HTML Elements programmatically using different types of HTML Elements.

We will be using a simple condition, where we will use a value from the component, which will be set programmatically.

Component Setup

Since we will be taking a look at how to disable Angular HTML Elements, we need a component to drive the data.

Our component will look like so:

 

@Component({

selector: 'app-root',

templateUrl: './app.component.html',

styleUrls: ['./app.component.scss'],

})

export class AppComponent {

public disableInputField = false;

public disableButtonField = false;

public disableTextareaField = false;

public disableMaterialInputField = false;

public disableSelectField = false;

}

 

How to disable Angular Input

All you need to do is to hook the disabled property to the two-way bound value of the component.

 

<input type="text"

[disabled]="disableInputField"

placeholder="Input Field" />

<button (click)="disableInputField = true">

Disable Angular Input Field

</button>

 

How to disable Angular button

For this, all you need to do is to hook the disabled property of the button to the component’s property like so:

 

<button (click)="disableButtonField = true">

Disable Angular Button

</button>

<button [disabled]="disableButtonField">

Disable Angular Button

</button>

 

How to disable Angular textarea

This is similar to how you would disable Angular input element. You can use this option:

 

<textarea

 type="text"

 [disabled]="disableTextareField"

 placeholder="Textarea Field"

></textarea>

<button (click)="disableTextareField = true">

 Disable Angular TextArea Field

</button>

 

How to disable Angular material field

First of all, you will need to wrap the input element inside mat-form-field and then simply do this:

 

<mat-form-field>

 <input

   matInput

   type="text"

   [disabled]="disableMaterialInputField"

   placeholder="Material Input Field"

 />

</mat-form-field>

<button (click)="disableMaterialInputField = true">

 Disable Angular Material Field

</button>

 

How to disable Angular select

For this one, always remember that the Angular select is what will have the bound value, and hence, it is the one which you should have as the target to disable.

You can do it like so:

 

<select

 name=""

 id=""

 [disabled]="disableSelectField"

 placeholder="Select Field"

>

 <option value="1">1</option>

 <option value="2">2</option>

 <option value="3">3</option>

</select>

<button (click)="disableSelectField = true">

 Disable Angular Select Field

</button>

 

Conclusion

As you can see, it is so easy to disable Angular HTML elements, as needed.

With that, happy coding and

God bless you!

Related Articles

A Practical Example To Validate Angular Input Fields With Joi

At times, we want to do a quick and custom validation of Angular inputs. Well, how let’s see how we can validate Angular input fields using Joi.

Jun 21, 2023

Views 15

Angular Fix MatFormField Must Contain A MatFormFieldControl

Whenever we are using Angular material, mostly Angular mat-form-field, we get the error that mat-form-field must contain a mat-form-field-control, well, let’s fix that.

Jun 27, 2023

Views 13

Have You Tried Angular Switch Yet

Angular 17 shipped with Angular @switch, and this was a game changer, when it comes to simplicity compared to the former ngSwitch directive. In this guide, we will see how easy it is to use this awesome addition to Angular!

Nov 19, 2024

Views 12

Angular JavaScript React CSS Django Python React Native Next JS State Management Django Rest Framework Unity