Angular 4 checkbox change value

AngularTypescript

Angular Problem Overview


how could you achieve in Angular 4 that when you register in a checkbox save an "A" or "B" value. As much as I try, he is only sending me true or false, I hope someone can help me.

registry.component.ts

  this.userForm = new FormGroup({
   state: new FormControl('',),
  });

registry.component.html

<div class="form-group">
  <label>State</label>
  <input type="checkbox"
         [(ngModel)]="isChecked"
         (change)="checkValue(isChecked?'A':'B')"
         formControlName="state"/>
</div>  

<pre>{{userForm.value | json}}</pre>

That way I can get the console to show the value I want (A or B) but in the JSON is still true or false.

Angular Solutions


Solution 1 - Angular

This it what you are looking for:

<input type="checkbox" [(ngModel)]="isChecked" (change)="checkValue(isChecked?'A':'B')" />

Inside your class:

checkValue(event: any){
   console.log(event);
}

Also include FormsModule in app.module.ts to make ngModel work !

Hope it Helps!

Solution 2 - Angular

Give a try on this,

Template

<input (change)="fieldsChange($event)" value="angular" type="checkbox"/>

Ts File

fieldsChange(values:any):void {
  console.log(values.currentTarget.checked);
}

Solution 3 - Angular

changed = (evt) => {    
this.isChecked = evt.target.checked;
}

<input type="checkbox" [checked]="checkbox" (change)="changed($event)" id="no"/>

Solution 4 - Angular

I am guessing that this is what something you are trying to achieve.

<input type="checkbox" value="a" (click)="click($event)">A
<input type="checkbox" value="b" (click)="click($event)">B

click(ev){
   console.log(ev.target.defaultValue);
}

Solution 5 - Angular

Another approach is to use ngModelChange:

Template:

<input type="checkbox" ngModel (ngModelChange)="onChecked(obj, $event)" />

Controller:

onChecked(obj: any, isChecked: boolean){
  console.log(obj, isChecked); // {}, true || false
}

I prefer this method because here you get the relevant object and true/false values of a checkbox.

Solution 6 - Angular

This works for me, angular 9:

component.html file:

<mat-checkbox (change)="checkValue($event)">text</mat-checkbox>

component.ts file:

checkValue(e){console.log(e.target.checked);}

Solution 7 - Angular

We can do below. I am using angular 12.

[Html]

<input type="checkbox" formControlName="lock" (change)="changeStatus($event)" />

[TS]

changeStatus(event:Event){
    const isChecked = (<HTMLInputElement>event.target).checked;
    console.log(isChecked)
}

Solution 8 - Angular

You can use this:

<input type="checkbox" [checked]="record.status" (change)="changeStatus(record.id,$event)">

And on your ts file,

changeStatus(id, e) {
    var status = e.target.checked;
    this.yourseverice.changeStatus(id, status).subscribe(result => {
        if (status)
            this.notify.success(this.l('AddedAsKeyPeople'));
        else
            this.notify.success(this.l('RemovedFromKeyPeople'));

    });
}

Here, record is the model for current row and status is boolean value.

Solution 9 - Angular

try this worked for me :

checkValue(event: any) {
	this.siteSelected.majeur = event;
}

Solution 10 - Angular

Inside your component class:

checkValue(event: any) {
  this.userForm.patchValue({
    state: event
  })
}

Now in controls you have value A or B

Solution 11 - Angular

You can use form controller valueChanges() listener and subscribe for its events, for example

> On your controller or .ts file

constructor(private _formBuilder: FormBuilder) { }

  ngOnInit(): void {
            this.yourForm = this.createFrom();
            this.listenForCheckboxChanges();
           
  }

  private createFrom() {
     return this._formBuilder.group(
          {
            CheckBox:[false],
    
          })
  }

  get yourCheckbox(){
    return this.conservationForm.controls['CheckBox'];
  }

  listenForCheckboxChanges() {
    this.yourCheckbox.valueChanges.subscribe(res=>{
      //console vaule of checkbox
      console.log(res)
  	});
  }

> on your view or .htm file

<div  [formGroup]="yourForm" fxLayoutGap="12px">
  <mat-checkbox  formControlName="CheckBox">Checkbox!</mat-checkbox>
</div>

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionJ. EduView Question on Stackoverflow
Solution 1 - AngularSHOHIL SETHIAView Answer on Stackoverflow
Solution 2 - AngularbalajivaishnavView Answer on Stackoverflow
Solution 3 - AngularVadivel SubramanianView Answer on Stackoverflow
Solution 4 - AngularAddWeb Solution Pvt LtdView Answer on Stackoverflow
Solution 5 - AngularAxel186View Answer on Stackoverflow
Solution 6 - AngularShubhamView Answer on Stackoverflow
Solution 7 - AngularProgInView Answer on Stackoverflow
Solution 8 - AngularAbdus Salam AzadView Answer on Stackoverflow
Solution 9 - AngularAzhar ElharView Answer on Stackoverflow
Solution 10 - AngularChetan UpretiView Answer on Stackoverflow
Solution 11 - AngularEyayu TeferaView Answer on Stackoverflow