ItSolutionStuff.com

Angular 11/10 Checkbox Example Tutorial

By Hardik Savani • October 20, 2023
Angular

This article will provide example of angular 10 checkbox example tutorial. you can see how to use checkbox in angular 11/10. We will look at example of checkbox in angular 10 forms. I’m going to show you about checkbox example in angular 10.

You can use reactive form with checkbox in angular 10 application.

In this example, i simply take website array variable with list of variable and display list of checkbox with website name. i also added on change event to get selected checkbox value for reactive form element. you can easily get all array of selected checkbox value on form submit.

So, let's follow bellow step to create simple example. you can also see bellow screen shot for checkbox in angular app.

Step 1: Import FormsModule

If you want to create form in angular app then you need to import FormsModule from @angular/forms library. so let's add following code to app.module.ts file.

src/app/app.module.ts

import { BrowserModule } from '@angular/platform-browser';

import { NgModule } from '@angular/core';

import { AppComponent } from './app.component';

import { FormsModule, ReactiveFormsModule } from '@angular/forms';

@NgModule({

declarations: [

AppComponent

],

imports: [

BrowserModule,

FormsModule,

ReactiveFormsModule

],

providers: [],

bootstrap: [AppComponent]

})

export class AppModule { }

Step 2: Form with ngModel

In this step, we will write code of html form with ngModel. so add following code to app.component.html file.

I used bootstrap class on this form. if you want to add than then follow this link too: Install Boorstrap 4 to Angular 10.

src/app/app.component.html

<div class="container">

<h1>Angular CheckBox Example - ItSolutionStuff.com</h1>

<form [formGroup]="form" (ngSubmit)="submit()">

<div class="form-group">

<label for="website">Website:</label>

<div *ngFor="let web of websiteList">

<label>

<input type="checkbox" [value]="web.id" (change)="onCheckboxChange($event)" />

{{web.name}}

</label>

</div>

</div>

<button class="btn btn-primary" type="submit" [disabled]="!form.valid">Submit</button>

</form>

</div>

Step 3: updated Ts File

In ts file. we will write submit() and get all input fields values. we need to import this libraries FormBuilder, FormGroup, FormControl, Validators, FormArray. so let's add following code to app.component.ts file.

src/app/app.component.ts

import { Component } from '@angular/core';

import { FormBuilder, FormGroup, FormControl, Validators, FormArray} from '@angular/forms';

@Component({

selector: 'app-root',

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

styleUrls: ['./app.component.css']

})

export class AppComponent {

form: FormGroup;

websiteList: any = [

{ id: 1, name: 'ItSolutionStuff.com' },

{ id: 2, name: 'HDTuto.com' },

{ id: 3, name: 'NiceSnippets.com' }

];

constructor(private formBuilder: FormBuilder) {

this.form = this.formBuilder.group({

website: this.formBuilder.array([], [Validators.required])

})

}

onCheckboxChange(e) {

const website: FormArray = this.form.get('website') as FormArray;

if (e.target.checked) {

website.push(new FormControl(e.target.value));

} else {

const index = website.controls.findIndex(x => x.value === e.target.value);

website.removeAt(index);

}

}

submit(){

console.log(this.form.value);

}

}

Now you can run your application using following command:

ng serve

You can check now.

I hope it can help you...

Hardik Savani

Hardik Savani

I'm a full-stack developer, entrepreneur, and founder of ItSolutionStuff.com. Passionate about PHP, Laravel, JavaScript, and helping developers grow.

šŸ“ŗ Subscribe on YouTube

We Are Recommending You

ā˜…

How to Create Service in Angular 10?

Read Now →
ā˜…

Angular 10 Image Upload with Preview Example

Read Now →
ā˜…

How to use FormGroup in Angular 11/10?

Read Now →
ā˜…

How to use Bootstrap Datepicker in Angular 11/10?

Read Now →
ā˜…

Angular 10 Bootstrap Modal Example

Read Now →
ā˜…

Angular 11/10 Global Variable for All Components Example

Read Now →
ā˜…

Angular 10 HttpClient Service Tutorial and Example

Read Now →
ā˜…

Angular 10 Router and Nested Routes Tutorial With Example

Read Now →
ā˜…

Angular 10 Install Material Design Example

Read Now →
ā˜…

How to Use Bootstrap 4 in Angular 10?

Read Now →
ā˜…

Angular 10 Reactive Forms Validation Example

Read Now →