How to bind to checkbox change/click event with Angular?

Today, We want to share with you How to bind to checkbox change/click event with Angular?.In this post we will show you Angular Checkbox Change Event Example, hear for angular input checkbox change event we will give you demo and example for implement.In this post, we will learn about call function on checkbox checked angular 6 with an example.

How to bind to checkbox change/click event with Angular?

There are the Following The simple About checkbox change event angular Full Information With Example and source code.

As I will cover this Post with live Working example to develop checkbox change event in angular 9, so the checkbox change event in angular is used for this example is following below.

Read Also:  Angular check ngIf Array is Empty/isset/Null

Step 1: Import FormsModule

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

src/app/app.component.html

<div class="container dsp">
<h1>Angularjs CheckBox Example - Pakainfo.com</h1>
<form>
<div class="form-group"><label for="bloglist">Website:</label>
<div><label>
<input id="" type="checkbox" value="" />
{{web.name}}
</label></div>
</div>
<button class="btn btn-primary" disabled="disabled" form="" type="submit">Submit</button>

</form></div>

Step 3: updated 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;
bloglistList: any = [
{ id: 1, name: 'pakainfo.com' },
{ id: 2, name: 'infinityknow.com' },
{ id: 3, name: 'ministackoverflow.com' }
];

constructor(private formBuilder: FormBuilder) {
this.form = this.formBuilder.group({
bloglist: this.formBuilder.array([], [Validators.required])
})
}

onCheckboxChange(e) {
const bloglist: FormArray = this.form.get('bloglist') as FormArray;

if (e.target.checked) {
bloglist.push(new FormControl(e.target.value));
} else {
const index = bloglist.controls.findIndex(x => x.value === e.target.value);
bloglist.removeAt(index);
}
}

submit(){
console.log(this.form.value);
}

}

Web Programming Tutorials Example with Demo

Read :

Read Also:  ng-init Directive using Angular Example with demo

Summary

You can also read about AngularJS, ASP.NET, VueJs, PHP.

I hope you get an idea about angular checkbox change event example.
I would like to have feedback on my infinityknow.com blog.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.