In this Video, I am going to discuss Checkbox in Angular Template Driven Forms in detail. Please read our previous Video as it is a continuation part to that Video where we discussed Radio Buttons in Angular Template Driven Forms. At the end of this Video, you will understand what are Checkbox controls and when and how to use Checkbox in Angular Applications.
How to implement Checkbox in Angular Template Driven Forms?. The Complete Angular Developer Course 2023 [Videos].
What is a Checkbox?
A Checkbox is an HTML element which allows the users to select multiple options from the available options. For example, in most of the website when you are filling a form you may find a checkbox for terms and conditions which needs to be accept in order to submit the form.
Example to understand Checkbox in Angular Template Driven Forms:
Let us understand how to create and use Checkbox in Angular Template Driven Forms. We are going to work with the same example that we worked in our previous Video.
Now, we want to include “Accept Terms & Conditions” checkbox in the student registration form as shown in the below image. When we select the Accept Terms & Conditions checkbox and when we click the “Submit” button, we want to display the selected value of the checkbox in the console. Here, if the use select the checkbox then true will be logged into the console else false will be logged into the console.
How to create check box in angular template driven forms?
Please have a look at the below code which will create Accept Terms and Conditions checkbox.
In the above code, we set the name attribute of the input element checkbox to isAccept. We have not set the value property here. This is because its value can be true of false. If the checkbox is checked or selected then the value is true else the value is false.
The complete code of app.component.html:
Following is the complete code of app.component.html file.
Modifying app.component.ts file:
We want to log the posted form values into the console tab. So, modify the app.component.ts file as shown below.
Save the changes and browse the application, then open browser developers tool by pressing F12 key and click on the console tab. Fill the form and click on the submit button and you should see the posted form values in the console tab as shown in the below image.
How to get a radio checkbox checked by default in Angular?
As we know when working with real-time applications, many a times we need to provide the checkbox to be checked by default when the form load initially and we normally do this by adding the checked attribute of the checkbox.
If we include the checked attribute on the checkbox, then we may expect that the checkbox to be checked by default when the page load. But in angular template driven forms, you will not get that default checked when the page loads initially.So, lets include the “checked” attribute on the checkbox and verify this. So. Modify the Checkbox HTML code as shown below.
<input type=”checkbox” name=”isAccept” ngModel checked>Accept Terms & Conditions
With the above changes in place, now browse the application and you will see the the checkbox is not checked by default when the page load.
However, if we remove the “ngModel” directive from the checkbox as shown below, then you will see that the checkbox is checked when the form is load.
<input type=”checkbox” name=”isAccept” checked>Accept Terms & Conditions
As we already discussed, in Angular, we generally use the “ngModel” directive for two-way data binding. So when we put the ngModel directive back into the control then the “checked” attribute will not work on the checkbox. If we remove the ngModel then checked attributes work but two way data binding will not work.
How to make it works?
In order to make it work, we need to include a property lets say “isAccept” in the component class and initialize its value to true. So.modify the app.component.ts file as shown below.
Modifying the app.component.html
Now we include to include the ngModel directive and bind it with the component property isAccept. To do so, modify the checkbox HTML code in the app.component.html file as shown below.
With the above changes in place, now browse the application, you should see the Accept Terms & Conditions checkbox is checked by default when the form loads initially.
Now, even if you remove the “checked” attribute from the checkbox, then it still checked by default when the form loads. This is possible because of the Angular two-way data binding. In our example, we do not want the checkbox to be checked by default, so we remove the “checked” attribute and the “isAccept” property from the component class and ngModel directive.
How to disable a Checkbox in Angular Template Driven Forms?
In order to disable a checkbox in Angular Template Driven Form, we need use the disabled attribute on the checkbox as shown below.
<input type=”checkbox” name=”isAccept” ngModel disabled>Accept Terms & Conditions
With the above changes, now it is not possible to select the checkbox. The most important point that you need to remember is, by default, the disabled form controls are not included in the Angular auto generated form model.
Since, the isAccept checkbox is disabled, it will not be included in the Angular generated form model. So, after the filling when you click on the button, in the console tab, you will not find the isAceept value as shown in the below image.
In our example, we do not want the checkbox to be disabled, so please remove the disabled attribute from the checkbox.