Search code examples
javascriptangulartypescriptcheckboxangular-forms

Toggle Angular checkbox with string values instead of boolean true/false


I have to create three checkboxes in my form and their values need to toggle as "optin" and "optout" (string) as I check/uncheck the checkbox. Also I need to check all the checkboxes by default for "optin" value. I cannot figure out any solution, any help would be really appreciated :( as I am fairly new to angular

Here is the code I am working on

component.html

<form [formGroup]="optOutForm" (ngSubmit)="submitOptFormValue()">
    <div class="form-group">

   <input type="checkbox" formControlName="optOutFlag1" id="privacy" aria-labelledby="check1">
   <label for="privacy" id="check1"> Checkbox 1</label><br><br>

   <input type="checkbox" formControlName="optOutFlag2" id="security" aria-labelledby="check2">
   <label for="security" id="check2"> Checkbox 2</label><br><br>

   <input type="checkbox" formControlName="optOutFlag3" id="consent" aria-labelledby="check3">
   <label for="consent" id="check3"> Checkbox 3</label><br><br>

   <button> Submit Preference </button>

</div>
</form>

component.ts file

optOutForm:FormGroup
this.optOutForm=this.fb.group({
    optOutFlag1:["optin",Validators.required],
    optOutFlag2:["optin",Validators.required],
    optOutFlag3:["optin",Validators.required]
});  

Solution

  • This solution is based on the solution link by Eliseo.

    The checkbox toggles values as "optin" or "optout" when checked/unchecked and is initialized as "optin" by default in .ts file

    component.html file

    <form [formGroup]="optOutForm" (ngSubmit)="submitOptFormValue()">
    
       <div class="form-group">
    
       <div class="optClass">
               <input  type="checkbox" 
               [ngModel]="optOutForm.get('optOutFlag1')?.value==='optin'"
               (ngModelChange)="optOutForm.get('optOutFlag1').setValue($event?'optin':'optout')"  
               [ngModelOptions]="{standalone:true}" id="privacy">
    
               <label "for="privacy" id="check1">Checkbox 1</label>
      </div>
    
     <div class="optClass">
               <input  type="checkbox" 
               [ngModel]="optOutForm.get('optOutFlag2')?.value==='optin'"
               (ngModelChange)="optOutForm.get('optOutFlag2').setValue($event?'optin':'optout')"  
               [ngModelOptions]="{standalone:true}" id="security">
    
               <label "for="security" id="check1">Checkbox 2</label>
      </div>
    
     <div class="optClass">
               <input  type="checkbox" 
               [ngModel]="optOutForm.get('optOutFlag3')?.value==='optin'"
               (ngModelChange)="optOutForm.get('optOutFlag3').setValue($event?'optin':'optout')"  
               [ngModelOptions]="{standalone:true}" id="consent">
    
               <label "for="consent" id="check1">Checkbox 3</label>
      </div>
    </div>
    </form>
    

    component.ts file

    optOutForm:FormGroup
    
    constructor(private fb:FormBuilder){}
    
    ngOnInit(){
     this.optOutForm=this.fb.group({
        optOutFlag1:['optin',Validators.required],
        optOutFlag2:['optin',Validators.required],
        optOutFlag3:['optin',Validators.required]
       });
    }