Angular 12/11 Checkbox Checked Event Tutorial

Angular 12/11 Checkbox Checked Event Tutorial

Angular 11/12 get a checked checkbox values example. In this tutorial, you will learn how to get checked checkbox values in the angular 11/12 app.

In this tutorial will guide you step by step on how to get checked checkbox value with reactive form in angular 11/12 app.

And as well as, take website array variable with list of variable and display list of checkbox with website name. Then add on change event to get selected checkbox value with reactive form element in angular 11/12 app.

Angular 12/11 Checkbox Checked Event Example

Follow the following steps and use checkbox checked event in angular 11/12 app:

  • Step 1 – Create New Angular App
  • Step 2 – Add Code on Module.ts File
  • Step 3 – Add Code on View File
  • Step 4 – Add Code On Component ts File
  • Step 5 – Start Angular App

Step 1 – Create New Angular App

First of all, open your terminal and execute the following command on it to install angular app:

ng new my-new-app

Step 2 – Add Code on Module.ts File

In this step, visit src/app directory and open app.module.ts file. Then add the following code into it:

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 3 – Add Code on View File

In this step, create simple reactive form for get selected checkbox values. So, visit src/app/app.component.html and update the following code into it:

<div class="container">
    <h1>Angular CheckBox Example - Tutsmake.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 4 – Add Code On Component ts File

In this step, visit the src/app directory and open app.component.ts. Then add the following code into component.ts file:

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: 'Google.com' },
    { id: 2, name: 'Angular.com' },
    { id: 3, name: 'Tutsmake.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);
  }
    
}

Step 5 – Start Angular App

In this step, execute the following commands on terminal to start angular app:

ng serve

php -S localhost:8001

Recommended Angular Posts

AuthorAdmin

My name is Devendra Dode. I am a full-stack developer, entrepreneur, and owner of Tutsmake.com. I like writing tutorials and tips that can help other developers. I share tutorials of PHP, Python, Javascript, JQuery, Laravel, Livewire, Codeigniter, Node JS, Express JS, Vue JS, Angular JS, React Js, MySQL, MongoDB, REST APIs, Windows, Xampp, Linux, Ubuntu, Amazon AWS, Composer, SEO, WordPress, SSL and Bootstrap from a starting stage. As well as demo example.

Leave a Reply

Your email address will not be published. Required fields are marked *