Wednesday, June 5, 2024
 Popular · Latest · Hot · Upcoming
7
rated 0 times [  14] [ 7]  / answers: 1 / hits: 6304  / 2 Years ago, wed, february 16, 2022, 12:00:00

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]
});

More From » angular

 Answers
1

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]
});
}

[#359] Tuesday, February 8, 2022, 2 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
kenyonmasonc

Total Points: 44
Total Questions: 117
Total Answers: 116

Location: Morocco
Member since Fri, May 22, 2020
4 Years ago
;