Radio button toggling not working in reactive formAngular - Is it possible to use a control's path instead of its name within nested reactive forms?Inject AbstractControlDirective instance as @ContentChild to custom directiveChange detection not working when updating FormArray inside FormGroupAngular 2+ get an Array of all FormControl elementsAngular 2/5 Conditional validation - reactive formERROR Error: Cannot find control with pathuse formControlName in a label or span?FormControlName not working with ion-radioReactive Form not updating with Third Party Address LookupToggle switch not working inside angular reactive form

Valid Badminton Score?

What is the difference between "behavior" and "behaviour"?

Why escape if the_content isnt?

Implement the Thanos sorting algorithm

Why are there no referendums in the US?

How do I find the solutions of the following equation?

What happens if you roll doubles 3 times then land on "Go to jail?"

How can I kill an app using Terminal?

How did Doctor Strange see the winning outcome in Avengers: Infinity War?

What is the best translation for "slot" in the context of multiplayer video games?

India just shot down a satellite from the ground. At what altitude range is the resulting debris field?

How to Reset Passwords on Multiple Websites Easily?

How easy is it to start Magic from scratch?

What can we do to stop prior company from asking us questions?

How to check is there any negative term in a large list?

Unreliable Magic - Is it worth it?

How do we know the LHC results are robust?

Do the temporary hit points from the Battlerager barbarian's Reckless Abandon stack if I make multiple attacks on my turn?

Large drywall patch supports

System.debug(JSON.Serialize(o)) Not longer shows full string

Class Action - which options I have?

Avoiding estate tax by giving multiple gifts

Is a stroke of luck acceptable after a series of unfavorable events?

How do scammers retract money, while you can’t?



Radio button toggling not working in reactive form


Angular - Is it possible to use a control's path instead of its name within nested reactive forms?Inject AbstractControlDirective instance as @ContentChild to custom directiveChange detection not working when updating FormArray inside FormGroupAngular 2+ get an Array of all FormControl elementsAngular 2/5 Conditional validation - reactive formERROR Error: Cannot find control with pathuse formControlName in a label or span?FormControlName not working with ion-radioReactive Form not updating with Third Party Address LookupToggle switch not working inside angular reactive form













3















I am developing the toggling radio button functionality in angular 7 using reactive forms. At the moment radio button is rendering for every form array but not toggling across the arrays. The user needs an ability to set one of the addresses as primary address. I have tried to assign new ids and value for the radio button but that has not made any difference



UI



[![enter image description here][1]][1]



Code - Update the code based on José Gazzano comments



<style>
.desc-header
background-color: #FAE7D6;
color: black


.header
width: 8%;



.panel-heading
color: white;
background-color: #F59850;
border-color: #ddd;


.col-form-label
padding: 0%;


.scrollClass
height: 500px;
overflow-y: scroll;
overflow-x: hidden;



</style>

<div class="card">
<div class="card-header panel-heading">
<span style="font-size: 18px; font-weight: bold; ">Firm Details</span>
<div class="pull-right" style="padding-right:10px;">
<label class="btn btn-primary" [ngClass]="'btn-primary': EditMode, 'btn-default': !EditMode "><input
type="checkbox" [(ngModel)]="EditMode" class="hidden">Edit Mode</label>

</div>
</div>
<div class="card-body">

<form [formGroup]="frmFirm" (ngSubmit)="saveManager()">
<div *ngIf="FirmDetails && FirmDetails.Firm" class="card-body scrollClass">

<div class="form-group row">
<label for="inputName" class="col-sm-2 col-form-label modal-label header">Name</label>
<div class="col-md-9">
<div *ngIf="!EditMode">FirmDetails.Firm.NAME</div>
<input *ngIf="EditMode" kendoTextBox [readonly]="false" class="form-control"
formControlName="NAME" />
</div>


</div>
<!--
<div class="form-group row">
<label for="inputTitle" class="col-md-1 col-form-label header">Short Name</label>
<div class="col-md-3">
<div *ngIf="!EditMode">FirmDetails.Firm.SHORT_NAME</div>
<input *ngIf="EditMode" kendoTextBox [readonly]="false" class="form-control"
formControlName="SHORT_NAME" />
</div>
</div>

<div class="form-group row">
<label for="inputEmail" class="col-md-1 col-form-label header">Alternate Name</label>
<div class="col-md-3">
<div *ngIf="!EditMode">FirmDetails.Firm.ALTERNATE_NAME</div>
<input *ngIf="EditMode" kendoTextBox [readonly]="false" class="form-control"
formControlName="ALTERNATE_NAME" />
</div>
</div> -->

<div class="form-group row">
<label for="inputEmail" class="col-sm-2 col-form-label modal-label header">Date Founded</label>
<div class="col-md-4">
<div *ngIf="!EditMode">dateFoundedDate</div>
<kendo-datepicker *ngIf="EditMode" [format]="'MMMM yyyy'"
(valueChange)="dateFoundedChanged($event)" formControlName="DATE_FOUNDED"
class="form-control">
</kendo-datepicker>
</div>
</div>




<div class="form-group row">
<label for="inputEmail" class="col-md-2 col-form-label modal-label header">Websites</label>
<div class="col-md-9">
<div class="form-group row">
<div class="col-md-4">
<label for="inputEmail">Website URL</label>
</div>
<div class="col-md-4">
<label for="inputEmail">User Name</label>
</div>
<div class="col-md-4">
<label for="inputEmail">Password</label>
</div>
</div>

</div>
<div class="col-md-9 offset-md-2">
<div formArrayName="Websites"
*ngFor="let item of frmFirm.get('Websites').controls; let i = index; let last = last">
<div [formGroupName]="i">

<div class="form-group row">
<div class="col-md-4">
<input formControlName="WEBSITE_URL" class="form-control">
</div>
<div class="col-md-4">
<input formControlName="USERNAME" class="form-control">
</div>
<div class="col-md-3">
<input formControlName="PASSWORD" class="form-control">
</div>
<div class="col-md-1" *ngIf="EditMode">
<button (click)="removeWebsite(i)">RM</button>
</div>

</div>
</div>
</div>

</div>
</div>
<div class="form-group row" *ngIf="EditMode">
<div class="col-md-2 offset-md-2">
<button type="button" (click)="addWebsite()">Add Website</button>
</div>
</div>


</div>
<div *ngIf="EditMode">
<button type="button" (click)="addAddress()">Add Address</button>
</div> -->



<div formArrayName="Addresses" *ngFor="let item of frmFirm.get('Addresses').controls; let i = index;">

<div [formGroupName]="test">

<div class="form-group row">
<label class="col-md-2 col-form-label"> Primary Address
</label>
<div class="col-md-2">
<div class="form-check">
<label class="form-check-label">
<input class="form-check-input" id="primary1Idi" type="radio" value="homei"
formControlName="IS_HEAD_OFFICE"> Home
</label>
</div>
</div>
</div>

<div class="form-group row">
<label class="col-md-2 col-form-label" attr.for="'street1Id' + i">Line 1
</label>
<div class="col-md-9">
<input class="form-control" id="'street1Id' + i" type="text"
formControlName="LINE1">

</div>
</div>

<div class="form-group row">
<label class="col-md-2 col-form-label" attr.for="'street2Id' + i">Line 2
</label>
<div class="col-md-9">
<input class="form-control" id="'street2Id' + i" type="text"
formControlName="LINE2">
</div>
</div>

<div class="form-group row">
<label class="col-md-2 col-form-label" attr.for="'street3Id' + i">Line 3
</label>
<div class="col-md-9">
<input class="form-control" id="'street3Id' + i" type="text"
formControlName="LINE3">
</div>
</div>
<div class="form-group row">
<label class="col-md-2 col-form-label" attr.for="'phoneId' + i">Phone
</label>
<div class="col-md-9">
<input class="form-control" id="'phoneId' + i" type="text"
formControlName="SWITCHBOARD_INT">
</div>
</div>

<div class="form-group row" *ngIf="EditMode">

<div class="col-md-9 offset-md-2">
<button type="button" (click)="removeAddress(i)">Remove Address</button>
</div>

</div>


</div>

</div>
<div class="form-group row " *ngIf="EditMode">
<div class="col-md-2 offset-md-2">
<button type="button" (click)="addAddress()">Add Address</button>
</div>
</div>

</div>





<div class="btn-toolbar" style="padding-top:40px;">

<span *ngIf="EditMode"><button type="submit" class="btn btn-primary btn-view-all btn mr-3">Save</button>

</span>
<span><button type="button" class="btn btn-primary btn-view-all btn mr-3"
(click)="cancelManager">Cancel</button>
</span>
<span><button type="button" style="float: right;" class="btn btn-primary btn-view-all"
(click)="deleteManager()">Delete</button>
</span>
</div>
</form>
</div>
</div>


Component



import Component, Injectable, NgZone, ViewEncapsulation, ViewChild, Input from '@angular/core';
import OnInit from '@angular/core';
import FirmService from '../services/firm.service';
import * as ClassicEditor from '@ckeditor/ckeditor5-build-classic';
import CommonDataService from '../services/common.data.service';
import FormGroup, FormControl, FormBuilder, FormArray from '@angular/forms';
import ListItem from '../models/listItem';



@Component(
selector: 'mgr-firm',
templateUrl: './firm.component.html'
)
export class FirmComponent implements OnInit
private Error: string;
public FirmDetails: any;
public EditMode: boolean;
public Editor = ClassicEditor;
public EditorConfig: string;
public originalContactList: any;
public originalCityList: any;
public events: string[] = [];
@Input() FirmId: number;
DateFoundedDate: Date;
public frmFirm: FormGroup;
public value: Date = new Date();



constructor(private _fb: FormBuilder, private firmService: FirmService, private commonDataService: CommonDataService)



ngOnInit()
this.initializeFormModel();
this.getFirmDetails();



initializeFormModel()
this.frmFirm = this._fb.group(
NAME: [''],
SHORT_NAME: [''],
ALTERNATE_NAME: [''],
DATE_FOUNDED: [''],

HISTORY_HTML: [''],
People: [''],
Websites: this._fb.array([
this.createWebsite()
]),
Addresses: this._fb.array([
this.createAddress()
])
);





public addWebsite(): void
this.Websites.push(this.createWebsite());




public removeWebsite(index: number): void
const websites = this.frmFirm.get('Websites') as FormArray;
if (websites.length === 1)

websites.reset();
return;

websites.removeAt(index);



private createWebsite(): FormGroup
return this._fb.group(
WEBSITE_URL: [''],
USERNAME: [''],
PASSWORD: ['']
);




public addAddress(): void
this.Addresses.push(this.createAddress());




public removeAddress(index: number): void
const addressess = this.frmFirm.get('Addresses') as FormArray;
if (addressess.length === 1)

addressess.reset();
return;

addressess.removeAt(index);


private createAddress(): FormGroup
return this._fb.group(
// city: [''],
LINE1: [''],
LINE2: [''],
LINE3: [''],
// zipCode: [''],
SWITCHBOARD_INT: ['']
);




get Websites(): FormArray
return <FormArray>this.frmFirm.get('Websites');


get Addresses(): FormArray
return <FormArray>this.frmFirm.get('Addresses');



get cities(): ListItem[]
return JSON.parse(this.FirmDetails.LongCitiesJson).map(x => new ListItem(x.CITY_ID, x.CITY_NAME, null));



setFormValues(FirmDetails: any)
this.frmFirm.patchValue(
NAME: FirmDetails.Firm.NAME,
SHORT_NAME: FirmDetails.Firm.SHORT_NAME,
ALTERNATE_NAME: FirmDetails.Firm.ALTERNATE_NAME,
DATE_FOUNDED: this.getDate(FirmDetails.Firm.DATE_FOUNDED),
HISTORY_HTML: FirmDetails.Firm.HISTORY_HTML,
People: FirmDetails.People

);
this.websites.patchValue(this.modifyFormGroupValues(FirmDetails.Websites, this.websitesMap))

this.websites.patchValue(this.modifyFormGroupValues(FirmDetails.Websites, this.websitesMap));

this.addressess.patchValue(this.modifyFormGroupValues(FirmDetails.Addresses,this.addressesMap))






const addressGroup = FirmDetails.Firm.Addresses.map(address =>
return this._fb.group(
ID: [address.ID],
LINE1: [address.LINE1],
LINE2: [address.LINE2],
LINE3: [address.LINE3],
SWITCHBOARD_INT: [address.SWITCHBOARD_INT],
IS_HEAD_OFFICE: [address.IS_HEAD_OFFICE],
CITY: [address.CITY],
CITY_ID: [address.CITY_ID],
POSTAL_CODE: [address.POSTAL_CODE],
FIRM_ID: [address.FIRM_ID]
);
);
const addressFormArray: FormArray = this._fb.array(addressGroup);
this.frmFirm.setControl('Addresses', addressFormArray);


const websiteGroup = FirmDetails.Firm.Websites.map(website =>
return this._fb.group(
ID: [website.ID],
WEBSITE_URL: [website.WEBSITE_URL],
USERNAME: [website.USERNAME],
PASSWORD: [website.PASSWORD],
FIRM_ID: [website.FIRM_ID]
);
);
const wesbiteFormArray: FormArray = this._fb.array(websiteGroup);
this.frmFirm.setControl('Websites', wesbiteFormArray);


getFirmDetails()
if (this.FirmId != null)
this.firmService.getFirmDetails(this.FirmId)
.subscribe(data =>
this.FirmDetails = data;
this.originalContactList = this.FirmDetails.People;
this.originalCityList = JSON.parse(this.FirmDetails.LongCitiesJson);
this.setFormValues(this.FirmDetails);
,
err =>
this.Error = 'An error has occurred. Please contact BSG';
,
() =>
);



get dateFoundedDate(): string
if (this.FirmDetails.Firm.DATE_FOUNDED != null)
const dateString = this.FirmDetails.Firm.DATE_FOUNDED;
const results = parseInt(dateString.replace(//Date(([0-9]+)[^+]//i, "$1"));
const date = new Date(results);
const month = date.toLocaleString('en-us', month: 'long' );
return (month + '-' + date.getFullYear());



private getDate(dateFounded: string): Date
if (dateFounded != null)
const results = parseInt(dateFounded.replace(//Date(([0-9]+)[^+]//i, "$1"));
const date = new Date(results);
return new Date(date.getFullYear() + '-' + (date.getMonth() + 1) + '-' + date.getDate());



saveManager()

const result = Object.assign(this.FirmDetails.Firm, this.frmFirm.value);
//this.FirmDetails.Firm = Object.assign(, result.Firm);


//const firm = ...this.FirmDetails.Firm, ...this.frmFirm.value;
this.firmService.createFirm(this.FirmDetails)
.subscribe(data =>
this.getFirmDetails();
this.EditMode = !this.EditMode;
,
err =>
this.Error = 'An error has occurred. Please contact BSG';
,
() =>
);


public keyContactChange(value: any): void
this.FirmDetails.Firm.KEY_CONTACT_ID = value;


public dateFoundedChanged(value: Date): void
this.FirmDetails.Firm.DATE_FOUNDED = value;



handleFilter(value)
if (value.length >= 1)
this.FirmDetails.People = this.originalContactList.filter((s) => s.FIRST_NAME.toLowerCase().indexOf(value.toLowerCase()) !== -1);
else
this.FirmDetails.People = this.originalContactList;














share|improve this question
























  • Can you please give a minimum reproduction on stackblitz

    – Sachin Gupta
    Mar 12 at 8:26















3















I am developing the toggling radio button functionality in angular 7 using reactive forms. At the moment radio button is rendering for every form array but not toggling across the arrays. The user needs an ability to set one of the addresses as primary address. I have tried to assign new ids and value for the radio button but that has not made any difference



UI



[![enter image description here][1]][1]



Code - Update the code based on José Gazzano comments



<style>
.desc-header
background-color: #FAE7D6;
color: black


.header
width: 8%;



.panel-heading
color: white;
background-color: #F59850;
border-color: #ddd;


.col-form-label
padding: 0%;


.scrollClass
height: 500px;
overflow-y: scroll;
overflow-x: hidden;



</style>

<div class="card">
<div class="card-header panel-heading">
<span style="font-size: 18px; font-weight: bold; ">Firm Details</span>
<div class="pull-right" style="padding-right:10px;">
<label class="btn btn-primary" [ngClass]="'btn-primary': EditMode, 'btn-default': !EditMode "><input
type="checkbox" [(ngModel)]="EditMode" class="hidden">Edit Mode</label>

</div>
</div>
<div class="card-body">

<form [formGroup]="frmFirm" (ngSubmit)="saveManager()">
<div *ngIf="FirmDetails && FirmDetails.Firm" class="card-body scrollClass">

<div class="form-group row">
<label for="inputName" class="col-sm-2 col-form-label modal-label header">Name</label>
<div class="col-md-9">
<div *ngIf="!EditMode">FirmDetails.Firm.NAME</div>
<input *ngIf="EditMode" kendoTextBox [readonly]="false" class="form-control"
formControlName="NAME" />
</div>


</div>
<!--
<div class="form-group row">
<label for="inputTitle" class="col-md-1 col-form-label header">Short Name</label>
<div class="col-md-3">
<div *ngIf="!EditMode">FirmDetails.Firm.SHORT_NAME</div>
<input *ngIf="EditMode" kendoTextBox [readonly]="false" class="form-control"
formControlName="SHORT_NAME" />
</div>
</div>

<div class="form-group row">
<label for="inputEmail" class="col-md-1 col-form-label header">Alternate Name</label>
<div class="col-md-3">
<div *ngIf="!EditMode">FirmDetails.Firm.ALTERNATE_NAME</div>
<input *ngIf="EditMode" kendoTextBox [readonly]="false" class="form-control"
formControlName="ALTERNATE_NAME" />
</div>
</div> -->

<div class="form-group row">
<label for="inputEmail" class="col-sm-2 col-form-label modal-label header">Date Founded</label>
<div class="col-md-4">
<div *ngIf="!EditMode">dateFoundedDate</div>
<kendo-datepicker *ngIf="EditMode" [format]="'MMMM yyyy'"
(valueChange)="dateFoundedChanged($event)" formControlName="DATE_FOUNDED"
class="form-control">
</kendo-datepicker>
</div>
</div>




<div class="form-group row">
<label for="inputEmail" class="col-md-2 col-form-label modal-label header">Websites</label>
<div class="col-md-9">
<div class="form-group row">
<div class="col-md-4">
<label for="inputEmail">Website URL</label>
</div>
<div class="col-md-4">
<label for="inputEmail">User Name</label>
</div>
<div class="col-md-4">
<label for="inputEmail">Password</label>
</div>
</div>

</div>
<div class="col-md-9 offset-md-2">
<div formArrayName="Websites"
*ngFor="let item of frmFirm.get('Websites').controls; let i = index; let last = last">
<div [formGroupName]="i">

<div class="form-group row">
<div class="col-md-4">
<input formControlName="WEBSITE_URL" class="form-control">
</div>
<div class="col-md-4">
<input formControlName="USERNAME" class="form-control">
</div>
<div class="col-md-3">
<input formControlName="PASSWORD" class="form-control">
</div>
<div class="col-md-1" *ngIf="EditMode">
<button (click)="removeWebsite(i)">RM</button>
</div>

</div>
</div>
</div>

</div>
</div>
<div class="form-group row" *ngIf="EditMode">
<div class="col-md-2 offset-md-2">
<button type="button" (click)="addWebsite()">Add Website</button>
</div>
</div>


</div>
<div *ngIf="EditMode">
<button type="button" (click)="addAddress()">Add Address</button>
</div> -->



<div formArrayName="Addresses" *ngFor="let item of frmFirm.get('Addresses').controls; let i = index;">

<div [formGroupName]="test">

<div class="form-group row">
<label class="col-md-2 col-form-label"> Primary Address
</label>
<div class="col-md-2">
<div class="form-check">
<label class="form-check-label">
<input class="form-check-input" id="primary1Idi" type="radio" value="homei"
formControlName="IS_HEAD_OFFICE"> Home
</label>
</div>
</div>
</div>

<div class="form-group row">
<label class="col-md-2 col-form-label" attr.for="'street1Id' + i">Line 1
</label>
<div class="col-md-9">
<input class="form-control" id="'street1Id' + i" type="text"
formControlName="LINE1">

</div>
</div>

<div class="form-group row">
<label class="col-md-2 col-form-label" attr.for="'street2Id' + i">Line 2
</label>
<div class="col-md-9">
<input class="form-control" id="'street2Id' + i" type="text"
formControlName="LINE2">
</div>
</div>

<div class="form-group row">
<label class="col-md-2 col-form-label" attr.for="'street3Id' + i">Line 3
</label>
<div class="col-md-9">
<input class="form-control" id="'street3Id' + i" type="text"
formControlName="LINE3">
</div>
</div>
<div class="form-group row">
<label class="col-md-2 col-form-label" attr.for="'phoneId' + i">Phone
</label>
<div class="col-md-9">
<input class="form-control" id="'phoneId' + i" type="text"
formControlName="SWITCHBOARD_INT">
</div>
</div>

<div class="form-group row" *ngIf="EditMode">

<div class="col-md-9 offset-md-2">
<button type="button" (click)="removeAddress(i)">Remove Address</button>
</div>

</div>


</div>

</div>
<div class="form-group row " *ngIf="EditMode">
<div class="col-md-2 offset-md-2">
<button type="button" (click)="addAddress()">Add Address</button>
</div>
</div>

</div>





<div class="btn-toolbar" style="padding-top:40px;">

<span *ngIf="EditMode"><button type="submit" class="btn btn-primary btn-view-all btn mr-3">Save</button>

</span>
<span><button type="button" class="btn btn-primary btn-view-all btn mr-3"
(click)="cancelManager">Cancel</button>
</span>
<span><button type="button" style="float: right;" class="btn btn-primary btn-view-all"
(click)="deleteManager()">Delete</button>
</span>
</div>
</form>
</div>
</div>


Component



import Component, Injectable, NgZone, ViewEncapsulation, ViewChild, Input from '@angular/core';
import OnInit from '@angular/core';
import FirmService from '../services/firm.service';
import * as ClassicEditor from '@ckeditor/ckeditor5-build-classic';
import CommonDataService from '../services/common.data.service';
import FormGroup, FormControl, FormBuilder, FormArray from '@angular/forms';
import ListItem from '../models/listItem';



@Component(
selector: 'mgr-firm',
templateUrl: './firm.component.html'
)
export class FirmComponent implements OnInit
private Error: string;
public FirmDetails: any;
public EditMode: boolean;
public Editor = ClassicEditor;
public EditorConfig: string;
public originalContactList: any;
public originalCityList: any;
public events: string[] = [];
@Input() FirmId: number;
DateFoundedDate: Date;
public frmFirm: FormGroup;
public value: Date = new Date();



constructor(private _fb: FormBuilder, private firmService: FirmService, private commonDataService: CommonDataService)



ngOnInit()
this.initializeFormModel();
this.getFirmDetails();



initializeFormModel()
this.frmFirm = this._fb.group(
NAME: [''],
SHORT_NAME: [''],
ALTERNATE_NAME: [''],
DATE_FOUNDED: [''],

HISTORY_HTML: [''],
People: [''],
Websites: this._fb.array([
this.createWebsite()
]),
Addresses: this._fb.array([
this.createAddress()
])
);





public addWebsite(): void
this.Websites.push(this.createWebsite());




public removeWebsite(index: number): void
const websites = this.frmFirm.get('Websites') as FormArray;
if (websites.length === 1)

websites.reset();
return;

websites.removeAt(index);



private createWebsite(): FormGroup
return this._fb.group(
WEBSITE_URL: [''],
USERNAME: [''],
PASSWORD: ['']
);




public addAddress(): void
this.Addresses.push(this.createAddress());




public removeAddress(index: number): void
const addressess = this.frmFirm.get('Addresses') as FormArray;
if (addressess.length === 1)

addressess.reset();
return;

addressess.removeAt(index);


private createAddress(): FormGroup
return this._fb.group(
// city: [''],
LINE1: [''],
LINE2: [''],
LINE3: [''],
// zipCode: [''],
SWITCHBOARD_INT: ['']
);




get Websites(): FormArray
return <FormArray>this.frmFirm.get('Websites');


get Addresses(): FormArray
return <FormArray>this.frmFirm.get('Addresses');



get cities(): ListItem[]
return JSON.parse(this.FirmDetails.LongCitiesJson).map(x => new ListItem(x.CITY_ID, x.CITY_NAME, null));



setFormValues(FirmDetails: any)
this.frmFirm.patchValue(
NAME: FirmDetails.Firm.NAME,
SHORT_NAME: FirmDetails.Firm.SHORT_NAME,
ALTERNATE_NAME: FirmDetails.Firm.ALTERNATE_NAME,
DATE_FOUNDED: this.getDate(FirmDetails.Firm.DATE_FOUNDED),
HISTORY_HTML: FirmDetails.Firm.HISTORY_HTML,
People: FirmDetails.People

);
this.websites.patchValue(this.modifyFormGroupValues(FirmDetails.Websites, this.websitesMap))

this.websites.patchValue(this.modifyFormGroupValues(FirmDetails.Websites, this.websitesMap));

this.addressess.patchValue(this.modifyFormGroupValues(FirmDetails.Addresses,this.addressesMap))






const addressGroup = FirmDetails.Firm.Addresses.map(address =>
return this._fb.group(
ID: [address.ID],
LINE1: [address.LINE1],
LINE2: [address.LINE2],
LINE3: [address.LINE3],
SWITCHBOARD_INT: [address.SWITCHBOARD_INT],
IS_HEAD_OFFICE: [address.IS_HEAD_OFFICE],
CITY: [address.CITY],
CITY_ID: [address.CITY_ID],
POSTAL_CODE: [address.POSTAL_CODE],
FIRM_ID: [address.FIRM_ID]
);
);
const addressFormArray: FormArray = this._fb.array(addressGroup);
this.frmFirm.setControl('Addresses', addressFormArray);


const websiteGroup = FirmDetails.Firm.Websites.map(website =>
return this._fb.group(
ID: [website.ID],
WEBSITE_URL: [website.WEBSITE_URL],
USERNAME: [website.USERNAME],
PASSWORD: [website.PASSWORD],
FIRM_ID: [website.FIRM_ID]
);
);
const wesbiteFormArray: FormArray = this._fb.array(websiteGroup);
this.frmFirm.setControl('Websites', wesbiteFormArray);


getFirmDetails()
if (this.FirmId != null)
this.firmService.getFirmDetails(this.FirmId)
.subscribe(data =>
this.FirmDetails = data;
this.originalContactList = this.FirmDetails.People;
this.originalCityList = JSON.parse(this.FirmDetails.LongCitiesJson);
this.setFormValues(this.FirmDetails);
,
err =>
this.Error = 'An error has occurred. Please contact BSG';
,
() =>
);



get dateFoundedDate(): string
if (this.FirmDetails.Firm.DATE_FOUNDED != null)
const dateString = this.FirmDetails.Firm.DATE_FOUNDED;
const results = parseInt(dateString.replace(//Date(([0-9]+)[^+]//i, "$1"));
const date = new Date(results);
const month = date.toLocaleString('en-us', month: 'long' );
return (month + '-' + date.getFullYear());



private getDate(dateFounded: string): Date
if (dateFounded != null)
const results = parseInt(dateFounded.replace(//Date(([0-9]+)[^+]//i, "$1"));
const date = new Date(results);
return new Date(date.getFullYear() + '-' + (date.getMonth() + 1) + '-' + date.getDate());



saveManager()

const result = Object.assign(this.FirmDetails.Firm, this.frmFirm.value);
//this.FirmDetails.Firm = Object.assign(, result.Firm);


//const firm = ...this.FirmDetails.Firm, ...this.frmFirm.value;
this.firmService.createFirm(this.FirmDetails)
.subscribe(data =>
this.getFirmDetails();
this.EditMode = !this.EditMode;
,
err =>
this.Error = 'An error has occurred. Please contact BSG';
,
() =>
);


public keyContactChange(value: any): void
this.FirmDetails.Firm.KEY_CONTACT_ID = value;


public dateFoundedChanged(value: Date): void
this.FirmDetails.Firm.DATE_FOUNDED = value;



handleFilter(value)
if (value.length >= 1)
this.FirmDetails.People = this.originalContactList.filter((s) => s.FIRST_NAME.toLowerCase().indexOf(value.toLowerCase()) !== -1);
else
this.FirmDetails.People = this.originalContactList;














share|improve this question
























  • Can you please give a minimum reproduction on stackblitz

    – Sachin Gupta
    Mar 12 at 8:26













3












3








3


0






I am developing the toggling radio button functionality in angular 7 using reactive forms. At the moment radio button is rendering for every form array but not toggling across the arrays. The user needs an ability to set one of the addresses as primary address. I have tried to assign new ids and value for the radio button but that has not made any difference



UI



[![enter image description here][1]][1]



Code - Update the code based on José Gazzano comments



<style>
.desc-header
background-color: #FAE7D6;
color: black


.header
width: 8%;



.panel-heading
color: white;
background-color: #F59850;
border-color: #ddd;


.col-form-label
padding: 0%;


.scrollClass
height: 500px;
overflow-y: scroll;
overflow-x: hidden;



</style>

<div class="card">
<div class="card-header panel-heading">
<span style="font-size: 18px; font-weight: bold; ">Firm Details</span>
<div class="pull-right" style="padding-right:10px;">
<label class="btn btn-primary" [ngClass]="'btn-primary': EditMode, 'btn-default': !EditMode "><input
type="checkbox" [(ngModel)]="EditMode" class="hidden">Edit Mode</label>

</div>
</div>
<div class="card-body">

<form [formGroup]="frmFirm" (ngSubmit)="saveManager()">
<div *ngIf="FirmDetails && FirmDetails.Firm" class="card-body scrollClass">

<div class="form-group row">
<label for="inputName" class="col-sm-2 col-form-label modal-label header">Name</label>
<div class="col-md-9">
<div *ngIf="!EditMode">FirmDetails.Firm.NAME</div>
<input *ngIf="EditMode" kendoTextBox [readonly]="false" class="form-control"
formControlName="NAME" />
</div>


</div>
<!--
<div class="form-group row">
<label for="inputTitle" class="col-md-1 col-form-label header">Short Name</label>
<div class="col-md-3">
<div *ngIf="!EditMode">FirmDetails.Firm.SHORT_NAME</div>
<input *ngIf="EditMode" kendoTextBox [readonly]="false" class="form-control"
formControlName="SHORT_NAME" />
</div>
</div>

<div class="form-group row">
<label for="inputEmail" class="col-md-1 col-form-label header">Alternate Name</label>
<div class="col-md-3">
<div *ngIf="!EditMode">FirmDetails.Firm.ALTERNATE_NAME</div>
<input *ngIf="EditMode" kendoTextBox [readonly]="false" class="form-control"
formControlName="ALTERNATE_NAME" />
</div>
</div> -->

<div class="form-group row">
<label for="inputEmail" class="col-sm-2 col-form-label modal-label header">Date Founded</label>
<div class="col-md-4">
<div *ngIf="!EditMode">dateFoundedDate</div>
<kendo-datepicker *ngIf="EditMode" [format]="'MMMM yyyy'"
(valueChange)="dateFoundedChanged($event)" formControlName="DATE_FOUNDED"
class="form-control">
</kendo-datepicker>
</div>
</div>




<div class="form-group row">
<label for="inputEmail" class="col-md-2 col-form-label modal-label header">Websites</label>
<div class="col-md-9">
<div class="form-group row">
<div class="col-md-4">
<label for="inputEmail">Website URL</label>
</div>
<div class="col-md-4">
<label for="inputEmail">User Name</label>
</div>
<div class="col-md-4">
<label for="inputEmail">Password</label>
</div>
</div>

</div>
<div class="col-md-9 offset-md-2">
<div formArrayName="Websites"
*ngFor="let item of frmFirm.get('Websites').controls; let i = index; let last = last">
<div [formGroupName]="i">

<div class="form-group row">
<div class="col-md-4">
<input formControlName="WEBSITE_URL" class="form-control">
</div>
<div class="col-md-4">
<input formControlName="USERNAME" class="form-control">
</div>
<div class="col-md-3">
<input formControlName="PASSWORD" class="form-control">
</div>
<div class="col-md-1" *ngIf="EditMode">
<button (click)="removeWebsite(i)">RM</button>
</div>

</div>
</div>
</div>

</div>
</div>
<div class="form-group row" *ngIf="EditMode">
<div class="col-md-2 offset-md-2">
<button type="button" (click)="addWebsite()">Add Website</button>
</div>
</div>


</div>
<div *ngIf="EditMode">
<button type="button" (click)="addAddress()">Add Address</button>
</div> -->



<div formArrayName="Addresses" *ngFor="let item of frmFirm.get('Addresses').controls; let i = index;">

<div [formGroupName]="test">

<div class="form-group row">
<label class="col-md-2 col-form-label"> Primary Address
</label>
<div class="col-md-2">
<div class="form-check">
<label class="form-check-label">
<input class="form-check-input" id="primary1Idi" type="radio" value="homei"
formControlName="IS_HEAD_OFFICE"> Home
</label>
</div>
</div>
</div>

<div class="form-group row">
<label class="col-md-2 col-form-label" attr.for="'street1Id' + i">Line 1
</label>
<div class="col-md-9">
<input class="form-control" id="'street1Id' + i" type="text"
formControlName="LINE1">

</div>
</div>

<div class="form-group row">
<label class="col-md-2 col-form-label" attr.for="'street2Id' + i">Line 2
</label>
<div class="col-md-9">
<input class="form-control" id="'street2Id' + i" type="text"
formControlName="LINE2">
</div>
</div>

<div class="form-group row">
<label class="col-md-2 col-form-label" attr.for="'street3Id' + i">Line 3
</label>
<div class="col-md-9">
<input class="form-control" id="'street3Id' + i" type="text"
formControlName="LINE3">
</div>
</div>
<div class="form-group row">
<label class="col-md-2 col-form-label" attr.for="'phoneId' + i">Phone
</label>
<div class="col-md-9">
<input class="form-control" id="'phoneId' + i" type="text"
formControlName="SWITCHBOARD_INT">
</div>
</div>

<div class="form-group row" *ngIf="EditMode">

<div class="col-md-9 offset-md-2">
<button type="button" (click)="removeAddress(i)">Remove Address</button>
</div>

</div>


</div>

</div>
<div class="form-group row " *ngIf="EditMode">
<div class="col-md-2 offset-md-2">
<button type="button" (click)="addAddress()">Add Address</button>
</div>
</div>

</div>





<div class="btn-toolbar" style="padding-top:40px;">

<span *ngIf="EditMode"><button type="submit" class="btn btn-primary btn-view-all btn mr-3">Save</button>

</span>
<span><button type="button" class="btn btn-primary btn-view-all btn mr-3"
(click)="cancelManager">Cancel</button>
</span>
<span><button type="button" style="float: right;" class="btn btn-primary btn-view-all"
(click)="deleteManager()">Delete</button>
</span>
</div>
</form>
</div>
</div>


Component



import Component, Injectable, NgZone, ViewEncapsulation, ViewChild, Input from '@angular/core';
import OnInit from '@angular/core';
import FirmService from '../services/firm.service';
import * as ClassicEditor from '@ckeditor/ckeditor5-build-classic';
import CommonDataService from '../services/common.data.service';
import FormGroup, FormControl, FormBuilder, FormArray from '@angular/forms';
import ListItem from '../models/listItem';



@Component(
selector: 'mgr-firm',
templateUrl: './firm.component.html'
)
export class FirmComponent implements OnInit
private Error: string;
public FirmDetails: any;
public EditMode: boolean;
public Editor = ClassicEditor;
public EditorConfig: string;
public originalContactList: any;
public originalCityList: any;
public events: string[] = [];
@Input() FirmId: number;
DateFoundedDate: Date;
public frmFirm: FormGroup;
public value: Date = new Date();



constructor(private _fb: FormBuilder, private firmService: FirmService, private commonDataService: CommonDataService)



ngOnInit()
this.initializeFormModel();
this.getFirmDetails();



initializeFormModel()
this.frmFirm = this._fb.group(
NAME: [''],
SHORT_NAME: [''],
ALTERNATE_NAME: [''],
DATE_FOUNDED: [''],

HISTORY_HTML: [''],
People: [''],
Websites: this._fb.array([
this.createWebsite()
]),
Addresses: this._fb.array([
this.createAddress()
])
);





public addWebsite(): void
this.Websites.push(this.createWebsite());




public removeWebsite(index: number): void
const websites = this.frmFirm.get('Websites') as FormArray;
if (websites.length === 1)

websites.reset();
return;

websites.removeAt(index);



private createWebsite(): FormGroup
return this._fb.group(
WEBSITE_URL: [''],
USERNAME: [''],
PASSWORD: ['']
);




public addAddress(): void
this.Addresses.push(this.createAddress());




public removeAddress(index: number): void
const addressess = this.frmFirm.get('Addresses') as FormArray;
if (addressess.length === 1)

addressess.reset();
return;

addressess.removeAt(index);


private createAddress(): FormGroup
return this._fb.group(
// city: [''],
LINE1: [''],
LINE2: [''],
LINE3: [''],
// zipCode: [''],
SWITCHBOARD_INT: ['']
);




get Websites(): FormArray
return <FormArray>this.frmFirm.get('Websites');


get Addresses(): FormArray
return <FormArray>this.frmFirm.get('Addresses');



get cities(): ListItem[]
return JSON.parse(this.FirmDetails.LongCitiesJson).map(x => new ListItem(x.CITY_ID, x.CITY_NAME, null));



setFormValues(FirmDetails: any)
this.frmFirm.patchValue(
NAME: FirmDetails.Firm.NAME,
SHORT_NAME: FirmDetails.Firm.SHORT_NAME,
ALTERNATE_NAME: FirmDetails.Firm.ALTERNATE_NAME,
DATE_FOUNDED: this.getDate(FirmDetails.Firm.DATE_FOUNDED),
HISTORY_HTML: FirmDetails.Firm.HISTORY_HTML,
People: FirmDetails.People

);
this.websites.patchValue(this.modifyFormGroupValues(FirmDetails.Websites, this.websitesMap))

this.websites.patchValue(this.modifyFormGroupValues(FirmDetails.Websites, this.websitesMap));

this.addressess.patchValue(this.modifyFormGroupValues(FirmDetails.Addresses,this.addressesMap))






const addressGroup = FirmDetails.Firm.Addresses.map(address =>
return this._fb.group(
ID: [address.ID],
LINE1: [address.LINE1],
LINE2: [address.LINE2],
LINE3: [address.LINE3],
SWITCHBOARD_INT: [address.SWITCHBOARD_INT],
IS_HEAD_OFFICE: [address.IS_HEAD_OFFICE],
CITY: [address.CITY],
CITY_ID: [address.CITY_ID],
POSTAL_CODE: [address.POSTAL_CODE],
FIRM_ID: [address.FIRM_ID]
);
);
const addressFormArray: FormArray = this._fb.array(addressGroup);
this.frmFirm.setControl('Addresses', addressFormArray);


const websiteGroup = FirmDetails.Firm.Websites.map(website =>
return this._fb.group(
ID: [website.ID],
WEBSITE_URL: [website.WEBSITE_URL],
USERNAME: [website.USERNAME],
PASSWORD: [website.PASSWORD],
FIRM_ID: [website.FIRM_ID]
);
);
const wesbiteFormArray: FormArray = this._fb.array(websiteGroup);
this.frmFirm.setControl('Websites', wesbiteFormArray);


getFirmDetails()
if (this.FirmId != null)
this.firmService.getFirmDetails(this.FirmId)
.subscribe(data =>
this.FirmDetails = data;
this.originalContactList = this.FirmDetails.People;
this.originalCityList = JSON.parse(this.FirmDetails.LongCitiesJson);
this.setFormValues(this.FirmDetails);
,
err =>
this.Error = 'An error has occurred. Please contact BSG';
,
() =>
);



get dateFoundedDate(): string
if (this.FirmDetails.Firm.DATE_FOUNDED != null)
const dateString = this.FirmDetails.Firm.DATE_FOUNDED;
const results = parseInt(dateString.replace(//Date(([0-9]+)[^+]//i, "$1"));
const date = new Date(results);
const month = date.toLocaleString('en-us', month: 'long' );
return (month + '-' + date.getFullYear());



private getDate(dateFounded: string): Date
if (dateFounded != null)
const results = parseInt(dateFounded.replace(//Date(([0-9]+)[^+]//i, "$1"));
const date = new Date(results);
return new Date(date.getFullYear() + '-' + (date.getMonth() + 1) + '-' + date.getDate());



saveManager()

const result = Object.assign(this.FirmDetails.Firm, this.frmFirm.value);
//this.FirmDetails.Firm = Object.assign(, result.Firm);


//const firm = ...this.FirmDetails.Firm, ...this.frmFirm.value;
this.firmService.createFirm(this.FirmDetails)
.subscribe(data =>
this.getFirmDetails();
this.EditMode = !this.EditMode;
,
err =>
this.Error = 'An error has occurred. Please contact BSG';
,
() =>
);


public keyContactChange(value: any): void
this.FirmDetails.Firm.KEY_CONTACT_ID = value;


public dateFoundedChanged(value: Date): void
this.FirmDetails.Firm.DATE_FOUNDED = value;



handleFilter(value)
if (value.length >= 1)
this.FirmDetails.People = this.originalContactList.filter((s) => s.FIRST_NAME.toLowerCase().indexOf(value.toLowerCase()) !== -1);
else
this.FirmDetails.People = this.originalContactList;














share|improve this question
















I am developing the toggling radio button functionality in angular 7 using reactive forms. At the moment radio button is rendering for every form array but not toggling across the arrays. The user needs an ability to set one of the addresses as primary address. I have tried to assign new ids and value for the radio button but that has not made any difference



UI



[![enter image description here][1]][1]



Code - Update the code based on José Gazzano comments



<style>
.desc-header
background-color: #FAE7D6;
color: black


.header
width: 8%;



.panel-heading
color: white;
background-color: #F59850;
border-color: #ddd;


.col-form-label
padding: 0%;


.scrollClass
height: 500px;
overflow-y: scroll;
overflow-x: hidden;



</style>

<div class="card">
<div class="card-header panel-heading">
<span style="font-size: 18px; font-weight: bold; ">Firm Details</span>
<div class="pull-right" style="padding-right:10px;">
<label class="btn btn-primary" [ngClass]="'btn-primary': EditMode, 'btn-default': !EditMode "><input
type="checkbox" [(ngModel)]="EditMode" class="hidden">Edit Mode</label>

</div>
</div>
<div class="card-body">

<form [formGroup]="frmFirm" (ngSubmit)="saveManager()">
<div *ngIf="FirmDetails && FirmDetails.Firm" class="card-body scrollClass">

<div class="form-group row">
<label for="inputName" class="col-sm-2 col-form-label modal-label header">Name</label>
<div class="col-md-9">
<div *ngIf="!EditMode">FirmDetails.Firm.NAME</div>
<input *ngIf="EditMode" kendoTextBox [readonly]="false" class="form-control"
formControlName="NAME" />
</div>


</div>
<!--
<div class="form-group row">
<label for="inputTitle" class="col-md-1 col-form-label header">Short Name</label>
<div class="col-md-3">
<div *ngIf="!EditMode">FirmDetails.Firm.SHORT_NAME</div>
<input *ngIf="EditMode" kendoTextBox [readonly]="false" class="form-control"
formControlName="SHORT_NAME" />
</div>
</div>

<div class="form-group row">
<label for="inputEmail" class="col-md-1 col-form-label header">Alternate Name</label>
<div class="col-md-3">
<div *ngIf="!EditMode">FirmDetails.Firm.ALTERNATE_NAME</div>
<input *ngIf="EditMode" kendoTextBox [readonly]="false" class="form-control"
formControlName="ALTERNATE_NAME" />
</div>
</div> -->

<div class="form-group row">
<label for="inputEmail" class="col-sm-2 col-form-label modal-label header">Date Founded</label>
<div class="col-md-4">
<div *ngIf="!EditMode">dateFoundedDate</div>
<kendo-datepicker *ngIf="EditMode" [format]="'MMMM yyyy'"
(valueChange)="dateFoundedChanged($event)" formControlName="DATE_FOUNDED"
class="form-control">
</kendo-datepicker>
</div>
</div>




<div class="form-group row">
<label for="inputEmail" class="col-md-2 col-form-label modal-label header">Websites</label>
<div class="col-md-9">
<div class="form-group row">
<div class="col-md-4">
<label for="inputEmail">Website URL</label>
</div>
<div class="col-md-4">
<label for="inputEmail">User Name</label>
</div>
<div class="col-md-4">
<label for="inputEmail">Password</label>
</div>
</div>

</div>
<div class="col-md-9 offset-md-2">
<div formArrayName="Websites"
*ngFor="let item of frmFirm.get('Websites').controls; let i = index; let last = last">
<div [formGroupName]="i">

<div class="form-group row">
<div class="col-md-4">
<input formControlName="WEBSITE_URL" class="form-control">
</div>
<div class="col-md-4">
<input formControlName="USERNAME" class="form-control">
</div>
<div class="col-md-3">
<input formControlName="PASSWORD" class="form-control">
</div>
<div class="col-md-1" *ngIf="EditMode">
<button (click)="removeWebsite(i)">RM</button>
</div>

</div>
</div>
</div>

</div>
</div>
<div class="form-group row" *ngIf="EditMode">
<div class="col-md-2 offset-md-2">
<button type="button" (click)="addWebsite()">Add Website</button>
</div>
</div>


</div>
<div *ngIf="EditMode">
<button type="button" (click)="addAddress()">Add Address</button>
</div> -->



<div formArrayName="Addresses" *ngFor="let item of frmFirm.get('Addresses').controls; let i = index;">

<div [formGroupName]="test">

<div class="form-group row">
<label class="col-md-2 col-form-label"> Primary Address
</label>
<div class="col-md-2">
<div class="form-check">
<label class="form-check-label">
<input class="form-check-input" id="primary1Idi" type="radio" value="homei"
formControlName="IS_HEAD_OFFICE"> Home
</label>
</div>
</div>
</div>

<div class="form-group row">
<label class="col-md-2 col-form-label" attr.for="'street1Id' + i">Line 1
</label>
<div class="col-md-9">
<input class="form-control" id="'street1Id' + i" type="text"
formControlName="LINE1">

</div>
</div>

<div class="form-group row">
<label class="col-md-2 col-form-label" attr.for="'street2Id' + i">Line 2
</label>
<div class="col-md-9">
<input class="form-control" id="'street2Id' + i" type="text"
formControlName="LINE2">
</div>
</div>

<div class="form-group row">
<label class="col-md-2 col-form-label" attr.for="'street3Id' + i">Line 3
</label>
<div class="col-md-9">
<input class="form-control" id="'street3Id' + i" type="text"
formControlName="LINE3">
</div>
</div>
<div class="form-group row">
<label class="col-md-2 col-form-label" attr.for="'phoneId' + i">Phone
</label>
<div class="col-md-9">
<input class="form-control" id="'phoneId' + i" type="text"
formControlName="SWITCHBOARD_INT">
</div>
</div>

<div class="form-group row" *ngIf="EditMode">

<div class="col-md-9 offset-md-2">
<button type="button" (click)="removeAddress(i)">Remove Address</button>
</div>

</div>


</div>

</div>
<div class="form-group row " *ngIf="EditMode">
<div class="col-md-2 offset-md-2">
<button type="button" (click)="addAddress()">Add Address</button>
</div>
</div>

</div>





<div class="btn-toolbar" style="padding-top:40px;">

<span *ngIf="EditMode"><button type="submit" class="btn btn-primary btn-view-all btn mr-3">Save</button>

</span>
<span><button type="button" class="btn btn-primary btn-view-all btn mr-3"
(click)="cancelManager">Cancel</button>
</span>
<span><button type="button" style="float: right;" class="btn btn-primary btn-view-all"
(click)="deleteManager()">Delete</button>
</span>
</div>
</form>
</div>
</div>


Component



import Component, Injectable, NgZone, ViewEncapsulation, ViewChild, Input from '@angular/core';
import OnInit from '@angular/core';
import FirmService from '../services/firm.service';
import * as ClassicEditor from '@ckeditor/ckeditor5-build-classic';
import CommonDataService from '../services/common.data.service';
import FormGroup, FormControl, FormBuilder, FormArray from '@angular/forms';
import ListItem from '../models/listItem';



@Component(
selector: 'mgr-firm',
templateUrl: './firm.component.html'
)
export class FirmComponent implements OnInit
private Error: string;
public FirmDetails: any;
public EditMode: boolean;
public Editor = ClassicEditor;
public EditorConfig: string;
public originalContactList: any;
public originalCityList: any;
public events: string[] = [];
@Input() FirmId: number;
DateFoundedDate: Date;
public frmFirm: FormGroup;
public value: Date = new Date();



constructor(private _fb: FormBuilder, private firmService: FirmService, private commonDataService: CommonDataService)



ngOnInit()
this.initializeFormModel();
this.getFirmDetails();



initializeFormModel()
this.frmFirm = this._fb.group(
NAME: [''],
SHORT_NAME: [''],
ALTERNATE_NAME: [''],
DATE_FOUNDED: [''],

HISTORY_HTML: [''],
People: [''],
Websites: this._fb.array([
this.createWebsite()
]),
Addresses: this._fb.array([
this.createAddress()
])
);





public addWebsite(): void
this.Websites.push(this.createWebsite());




public removeWebsite(index: number): void
const websites = this.frmFirm.get('Websites') as FormArray;
if (websites.length === 1)

websites.reset();
return;

websites.removeAt(index);



private createWebsite(): FormGroup
return this._fb.group(
WEBSITE_URL: [''],
USERNAME: [''],
PASSWORD: ['']
);




public addAddress(): void
this.Addresses.push(this.createAddress());




public removeAddress(index: number): void
const addressess = this.frmFirm.get('Addresses') as FormArray;
if (addressess.length === 1)

addressess.reset();
return;

addressess.removeAt(index);


private createAddress(): FormGroup
return this._fb.group(
// city: [''],
LINE1: [''],
LINE2: [''],
LINE3: [''],
// zipCode: [''],
SWITCHBOARD_INT: ['']
);




get Websites(): FormArray
return <FormArray>this.frmFirm.get('Websites');


get Addresses(): FormArray
return <FormArray>this.frmFirm.get('Addresses');



get cities(): ListItem[]
return JSON.parse(this.FirmDetails.LongCitiesJson).map(x => new ListItem(x.CITY_ID, x.CITY_NAME, null));



setFormValues(FirmDetails: any)
this.frmFirm.patchValue(
NAME: FirmDetails.Firm.NAME,
SHORT_NAME: FirmDetails.Firm.SHORT_NAME,
ALTERNATE_NAME: FirmDetails.Firm.ALTERNATE_NAME,
DATE_FOUNDED: this.getDate(FirmDetails.Firm.DATE_FOUNDED),
HISTORY_HTML: FirmDetails.Firm.HISTORY_HTML,
People: FirmDetails.People

);
this.websites.patchValue(this.modifyFormGroupValues(FirmDetails.Websites, this.websitesMap))

this.websites.patchValue(this.modifyFormGroupValues(FirmDetails.Websites, this.websitesMap));

this.addressess.patchValue(this.modifyFormGroupValues(FirmDetails.Addresses,this.addressesMap))






const addressGroup = FirmDetails.Firm.Addresses.map(address =>
return this._fb.group(
ID: [address.ID],
LINE1: [address.LINE1],
LINE2: [address.LINE2],
LINE3: [address.LINE3],
SWITCHBOARD_INT: [address.SWITCHBOARD_INT],
IS_HEAD_OFFICE: [address.IS_HEAD_OFFICE],
CITY: [address.CITY],
CITY_ID: [address.CITY_ID],
POSTAL_CODE: [address.POSTAL_CODE],
FIRM_ID: [address.FIRM_ID]
);
);
const addressFormArray: FormArray = this._fb.array(addressGroup);
this.frmFirm.setControl('Addresses', addressFormArray);


const websiteGroup = FirmDetails.Firm.Websites.map(website =>
return this._fb.group(
ID: [website.ID],
WEBSITE_URL: [website.WEBSITE_URL],
USERNAME: [website.USERNAME],
PASSWORD: [website.PASSWORD],
FIRM_ID: [website.FIRM_ID]
);
);
const wesbiteFormArray: FormArray = this._fb.array(websiteGroup);
this.frmFirm.setControl('Websites', wesbiteFormArray);


getFirmDetails()
if (this.FirmId != null)
this.firmService.getFirmDetails(this.FirmId)
.subscribe(data =>
this.FirmDetails = data;
this.originalContactList = this.FirmDetails.People;
this.originalCityList = JSON.parse(this.FirmDetails.LongCitiesJson);
this.setFormValues(this.FirmDetails);
,
err =>
this.Error = 'An error has occurred. Please contact BSG';
,
() =>
);



get dateFoundedDate(): string
if (this.FirmDetails.Firm.DATE_FOUNDED != null)
const dateString = this.FirmDetails.Firm.DATE_FOUNDED;
const results = parseInt(dateString.replace(//Date(([0-9]+)[^+]//i, "$1"));
const date = new Date(results);
const month = date.toLocaleString('en-us', month: 'long' );
return (month + '-' + date.getFullYear());



private getDate(dateFounded: string): Date
if (dateFounded != null)
const results = parseInt(dateFounded.replace(//Date(([0-9]+)[^+]//i, "$1"));
const date = new Date(results);
return new Date(date.getFullYear() + '-' + (date.getMonth() + 1) + '-' + date.getDate());



saveManager()

const result = Object.assign(this.FirmDetails.Firm, this.frmFirm.value);
//this.FirmDetails.Firm = Object.assign(, result.Firm);


//const firm = ...this.FirmDetails.Firm, ...this.frmFirm.value;
this.firmService.createFirm(this.FirmDetails)
.subscribe(data =>
this.getFirmDetails();
this.EditMode = !this.EditMode;
,
err =>
this.Error = 'An error has occurred. Please contact BSG';
,
() =>
);


public keyContactChange(value: any): void
this.FirmDetails.Firm.KEY_CONTACT_ID = value;


public dateFoundedChanged(value: Date): void
this.FirmDetails.Firm.DATE_FOUNDED = value;



handleFilter(value)
if (value.length >= 1)
this.FirmDetails.People = this.originalContactList.filter((s) => s.FIRST_NAME.toLowerCase().indexOf(value.toLowerCase()) !== -1);
else
this.FirmDetails.People = this.originalContactList;











angular angular-reactive-forms






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Mar 12 at 14:54







Tom

















asked Mar 7 at 12:51









TomTom

63473178




63473178












  • Can you please give a minimum reproduction on stackblitz

    – Sachin Gupta
    Mar 12 at 8:26

















  • Can you please give a minimum reproduction on stackblitz

    – Sachin Gupta
    Mar 12 at 8:26
















Can you please give a minimum reproduction on stackblitz

– Sachin Gupta
Mar 12 at 8:26





Can you please give a minimum reproduction on stackblitz

– Sachin Gupta
Mar 12 at 8:26












3 Answers
3






active

oldest

votes


















3





+25









you set formControlName in the correct way, now just give a unic ID to each one of the radio buttons.



Edit:
plus you didn't closed your input tag.
I changed from <input type....> to <input type.../>



One more big tip:
When you are working with Angular, you should divide your html to much smaller components.
your html file is way too long then it should be.
for example your four group should be a single component that made from smaller one's as the input+label for example. the smaller component must receive all of the input that it's need for working with forms.
you can read more about how to do it in here



 <div class="form-group row">
<label class="col-md-2 col-form-label"> Primary Address </label>
<div class="col-md-2">
<div class="form-check">
<label class="form-check-label">
<input class="form-check-input" id="primary1Idi" type="radio" value="homei"
formControlName="IS_HEAD_OFFICE"/> <span>Home</span>
</label>
</div>
</div>
</div>





share|improve this answer

























  • I guess this is the only change that your a referring to primary1Idi for radio button control. I tried it doesn't seem to work

    – Tom
    Mar 9 at 14:49











  • @Tom please edit your question and upload the full html+TS so I will be able to simulate it on my own machine

    – Or Yaacov
    Mar 9 at 14:58











  • I have updated both the html and ts file.

    – Tom
    Mar 10 at 0:03











  • Were you able to spot the issue ?

    – Tom
    Mar 15 at 10:55


















3














the problem here is that your radio buttons have the same value assigned, so they are treated as the same input. That's why you can't toggle between them.



Try setting a different value for each of them and it should be working as expected and this will also help with your form logic and validations ;)



As a reminder, the only thing you need in order to make a group of radio buttons to toggle between them is setting the same name (or formControlName in your case) to all of them, and of course also different values. Do not set the same Id as it was previously suggested because that's not semantically correct from an HTML perspective



Here's a minimal StackBlitz example where you can try it out https://stackblitz.com/edit/angular-lvahzg?file=src%2Fapp%2Fapp.component.ts



It has all the radio buttons with the same formContrlName and the same value, try setting different values to each of them and see how it works ;)



Edit: Adding Angular documentation regarding radio buttons withing reactive forms https://angular.io/api/forms/RadioControlValueAccessor



Edit 2: Here's the issue I was talking on my comment



<div formArrayName="Addresses" *ngFor="let item of frmFirm.get('Addresses').controls; let i = index;">

<div [formGroupName]="i"> --> This formGroupName should be the same on each loop in order to make the radio buttons to toggle between them

<div class="form-group row">
<label class="col-md-2 col-form-label"> Primary Address
</label>
<div class="col-md-2">
<div class="form-check">
<label class="form-check-label">
<input class="form-check-input" id="primary1Idi" type="radio" value="homei"
formControlName="IS_HEAD_OFFICE"> Home
</label>
</div>
</div>
</div>





share|improve this answer

























  • Hi jose what you shared is a simple scenario. I tried providing different values. Not sure if the way i have done is right. Please find the updated code. However it is not yet working for me

    – Tom
    Mar 10 at 0:05











  • Hi @Tom I noticed another issue, your radio buttons are assigned to different forms. If you take close attention to your ngFor you're setting a different formGroupName on each iteration, and on each iteration defining the radio buttons. Since they're inside elements with different formGroupName, the reactive forms directive from angular treats them as different elements and so the value isn't toggling as expected. Try setting the same formGroupName on the form that contains the addresses. I'll update my original answer with this

    – José Gazzano
    Mar 10 at 13:39











  • Hey Jose, I have updated the html bit on the post. I have given the same formGroupName as Test but that doesnt seem to make any difference

    – Tom
    Mar 12 at 14:56











  • Were you able to spot the issue ?

    – Tom
    Mar 15 at 10:55











  • Hey @Tom I couldn't get to replicate that. But I'll try again if I manage to get some spare time. Good luck!

    – José Gazzano
    Mar 15 at 15:48


















0














My understanding of your requirements is when a user sets an address as "Home" all of the other addresses should be unset as "Home." If my understanding is correct, You were on the right track with your original code but there are a couple of things that you will need to correct.



Form Group Name



With the ngFor you are creating an address for each control that is in the FormArray. Each control in the FormArray is a FormGroup whos name is its index in the FormArray. So you were correct when you had i has the formGroupName



So change back to this



<div [formGroupName]="i">


Create an OnPrimaryToggleMethod



Add a method to the component that will set IS_HEAD_OFFICE to false for every other control in the array



onPrimaryToggled(newValue: any, changedItem: FormControl) 
if (newValue.returnValue === true)
const toChange = this.frmFirm.get('Addresses').controls.filter(el => el !== changedItem);
toChange.forEach(el => el.patchValue(
IS_HEAD_OFFICE: false
));




Update the checkbox to call the toggle method



Add a change event binding to the the radio button to call the method



(change)="onPrimaryToggled($event, item)"


Now it should look like the below code (btw, you don't need the value attribute)



<div formArrayName="Addresses" *ngFor="let item of frmFirm.get('Addresses').controls; let i = index;">
<div [formGroupName]="i">
<div class="form-group row">
<label class="col-md-2 col-form-label"> Primary Address </label>
<div class="col-md-2">
<div class="form-check">
<label class="form-check-label">

<input
class="form-check-input"
id="primary1Idi"
type="radio"
formControlName="IS_HEAD_OFFICE"
(change)="OnPrimaryToggled($event, item)"
> Home

</label>
</div>
</div>
</div>
</div>
</div>





share|improve this answer






















    Your Answer






    StackExchange.ifUsing("editor", function ()
    StackExchange.using("externalEditor", function ()
    StackExchange.using("snippets", function ()
    StackExchange.snippets.init();
    );
    );
    , "code-snippets");

    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "1"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader:
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    ,
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













    draft saved

    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55044248%2fradio-button-toggling-not-working-in-reactive-form%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    3 Answers
    3






    active

    oldest

    votes








    3 Answers
    3






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    3





    +25









    you set formControlName in the correct way, now just give a unic ID to each one of the radio buttons.



    Edit:
    plus you didn't closed your input tag.
    I changed from <input type....> to <input type.../>



    One more big tip:
    When you are working with Angular, you should divide your html to much smaller components.
    your html file is way too long then it should be.
    for example your four group should be a single component that made from smaller one's as the input+label for example. the smaller component must receive all of the input that it's need for working with forms.
    you can read more about how to do it in here



     <div class="form-group row">
    <label class="col-md-2 col-form-label"> Primary Address </label>
    <div class="col-md-2">
    <div class="form-check">
    <label class="form-check-label">
    <input class="form-check-input" id="primary1Idi" type="radio" value="homei"
    formControlName="IS_HEAD_OFFICE"/> <span>Home</span>
    </label>
    </div>
    </div>
    </div>





    share|improve this answer

























    • I guess this is the only change that your a referring to primary1Idi for radio button control. I tried it doesn't seem to work

      – Tom
      Mar 9 at 14:49











    • @Tom please edit your question and upload the full html+TS so I will be able to simulate it on my own machine

      – Or Yaacov
      Mar 9 at 14:58











    • I have updated both the html and ts file.

      – Tom
      Mar 10 at 0:03











    • Were you able to spot the issue ?

      – Tom
      Mar 15 at 10:55















    3





    +25









    you set formControlName in the correct way, now just give a unic ID to each one of the radio buttons.



    Edit:
    plus you didn't closed your input tag.
    I changed from <input type....> to <input type.../>



    One more big tip:
    When you are working with Angular, you should divide your html to much smaller components.
    your html file is way too long then it should be.
    for example your four group should be a single component that made from smaller one's as the input+label for example. the smaller component must receive all of the input that it's need for working with forms.
    you can read more about how to do it in here



     <div class="form-group row">
    <label class="col-md-2 col-form-label"> Primary Address </label>
    <div class="col-md-2">
    <div class="form-check">
    <label class="form-check-label">
    <input class="form-check-input" id="primary1Idi" type="radio" value="homei"
    formControlName="IS_HEAD_OFFICE"/> <span>Home</span>
    </label>
    </div>
    </div>
    </div>





    share|improve this answer

























    • I guess this is the only change that your a referring to primary1Idi for radio button control. I tried it doesn't seem to work

      – Tom
      Mar 9 at 14:49











    • @Tom please edit your question and upload the full html+TS so I will be able to simulate it on my own machine

      – Or Yaacov
      Mar 9 at 14:58











    • I have updated both the html and ts file.

      – Tom
      Mar 10 at 0:03











    • Were you able to spot the issue ?

      – Tom
      Mar 15 at 10:55













    3





    +25







    3





    +25



    3




    +25





    you set formControlName in the correct way, now just give a unic ID to each one of the radio buttons.



    Edit:
    plus you didn't closed your input tag.
    I changed from <input type....> to <input type.../>



    One more big tip:
    When you are working with Angular, you should divide your html to much smaller components.
    your html file is way too long then it should be.
    for example your four group should be a single component that made from smaller one's as the input+label for example. the smaller component must receive all of the input that it's need for working with forms.
    you can read more about how to do it in here



     <div class="form-group row">
    <label class="col-md-2 col-form-label"> Primary Address </label>
    <div class="col-md-2">
    <div class="form-check">
    <label class="form-check-label">
    <input class="form-check-input" id="primary1Idi" type="radio" value="homei"
    formControlName="IS_HEAD_OFFICE"/> <span>Home</span>
    </label>
    </div>
    </div>
    </div>





    share|improve this answer















    you set formControlName in the correct way, now just give a unic ID to each one of the radio buttons.



    Edit:
    plus you didn't closed your input tag.
    I changed from <input type....> to <input type.../>



    One more big tip:
    When you are working with Angular, you should divide your html to much smaller components.
    your html file is way too long then it should be.
    for example your four group should be a single component that made from smaller one's as the input+label for example. the smaller component must receive all of the input that it's need for working with forms.
    you can read more about how to do it in here



     <div class="form-group row">
    <label class="col-md-2 col-form-label"> Primary Address </label>
    <div class="col-md-2">
    <div class="form-check">
    <label class="form-check-label">
    <input class="form-check-input" id="primary1Idi" type="radio" value="homei"
    formControlName="IS_HEAD_OFFICE"/> <span>Home</span>
    </label>
    </div>
    </div>
    </div>






    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Mar 10 at 5:47

























    answered Mar 9 at 13:17









    Or YaacovOr Yaacov

    870321




    870321












    • I guess this is the only change that your a referring to primary1Idi for radio button control. I tried it doesn't seem to work

      – Tom
      Mar 9 at 14:49











    • @Tom please edit your question and upload the full html+TS so I will be able to simulate it on my own machine

      – Or Yaacov
      Mar 9 at 14:58











    • I have updated both the html and ts file.

      – Tom
      Mar 10 at 0:03











    • Were you able to spot the issue ?

      – Tom
      Mar 15 at 10:55

















    • I guess this is the only change that your a referring to primary1Idi for radio button control. I tried it doesn't seem to work

      – Tom
      Mar 9 at 14:49











    • @Tom please edit your question and upload the full html+TS so I will be able to simulate it on my own machine

      – Or Yaacov
      Mar 9 at 14:58











    • I have updated both the html and ts file.

      – Tom
      Mar 10 at 0:03











    • Were you able to spot the issue ?

      – Tom
      Mar 15 at 10:55
















    I guess this is the only change that your a referring to primary1Idi for radio button control. I tried it doesn't seem to work

    – Tom
    Mar 9 at 14:49





    I guess this is the only change that your a referring to primary1Idi for radio button control. I tried it doesn't seem to work

    – Tom
    Mar 9 at 14:49













    @Tom please edit your question and upload the full html+TS so I will be able to simulate it on my own machine

    – Or Yaacov
    Mar 9 at 14:58





    @Tom please edit your question and upload the full html+TS so I will be able to simulate it on my own machine

    – Or Yaacov
    Mar 9 at 14:58













    I have updated both the html and ts file.

    – Tom
    Mar 10 at 0:03





    I have updated both the html and ts file.

    – Tom
    Mar 10 at 0:03













    Were you able to spot the issue ?

    – Tom
    Mar 15 at 10:55





    Were you able to spot the issue ?

    – Tom
    Mar 15 at 10:55













    3














    the problem here is that your radio buttons have the same value assigned, so they are treated as the same input. That's why you can't toggle between them.



    Try setting a different value for each of them and it should be working as expected and this will also help with your form logic and validations ;)



    As a reminder, the only thing you need in order to make a group of radio buttons to toggle between them is setting the same name (or formControlName in your case) to all of them, and of course also different values. Do not set the same Id as it was previously suggested because that's not semantically correct from an HTML perspective



    Here's a minimal StackBlitz example where you can try it out https://stackblitz.com/edit/angular-lvahzg?file=src%2Fapp%2Fapp.component.ts



    It has all the radio buttons with the same formContrlName and the same value, try setting different values to each of them and see how it works ;)



    Edit: Adding Angular documentation regarding radio buttons withing reactive forms https://angular.io/api/forms/RadioControlValueAccessor



    Edit 2: Here's the issue I was talking on my comment



    <div formArrayName="Addresses" *ngFor="let item of frmFirm.get('Addresses').controls; let i = index;">

    <div [formGroupName]="i"> --> This formGroupName should be the same on each loop in order to make the radio buttons to toggle between them

    <div class="form-group row">
    <label class="col-md-2 col-form-label"> Primary Address
    </label>
    <div class="col-md-2">
    <div class="form-check">
    <label class="form-check-label">
    <input class="form-check-input" id="primary1Idi" type="radio" value="homei"
    formControlName="IS_HEAD_OFFICE"> Home
    </label>
    </div>
    </div>
    </div>





    share|improve this answer

























    • Hi jose what you shared is a simple scenario. I tried providing different values. Not sure if the way i have done is right. Please find the updated code. However it is not yet working for me

      – Tom
      Mar 10 at 0:05











    • Hi @Tom I noticed another issue, your radio buttons are assigned to different forms. If you take close attention to your ngFor you're setting a different formGroupName on each iteration, and on each iteration defining the radio buttons. Since they're inside elements with different formGroupName, the reactive forms directive from angular treats them as different elements and so the value isn't toggling as expected. Try setting the same formGroupName on the form that contains the addresses. I'll update my original answer with this

      – José Gazzano
      Mar 10 at 13:39











    • Hey Jose, I have updated the html bit on the post. I have given the same formGroupName as Test but that doesnt seem to make any difference

      – Tom
      Mar 12 at 14:56











    • Were you able to spot the issue ?

      – Tom
      Mar 15 at 10:55











    • Hey @Tom I couldn't get to replicate that. But I'll try again if I manage to get some spare time. Good luck!

      – José Gazzano
      Mar 15 at 15:48















    3














    the problem here is that your radio buttons have the same value assigned, so they are treated as the same input. That's why you can't toggle between them.



    Try setting a different value for each of them and it should be working as expected and this will also help with your form logic and validations ;)



    As a reminder, the only thing you need in order to make a group of radio buttons to toggle between them is setting the same name (or formControlName in your case) to all of them, and of course also different values. Do not set the same Id as it was previously suggested because that's not semantically correct from an HTML perspective



    Here's a minimal StackBlitz example where you can try it out https://stackblitz.com/edit/angular-lvahzg?file=src%2Fapp%2Fapp.component.ts



    It has all the radio buttons with the same formContrlName and the same value, try setting different values to each of them and see how it works ;)



    Edit: Adding Angular documentation regarding radio buttons withing reactive forms https://angular.io/api/forms/RadioControlValueAccessor



    Edit 2: Here's the issue I was talking on my comment



    <div formArrayName="Addresses" *ngFor="let item of frmFirm.get('Addresses').controls; let i = index;">

    <div [formGroupName]="i"> --> This formGroupName should be the same on each loop in order to make the radio buttons to toggle between them

    <div class="form-group row">
    <label class="col-md-2 col-form-label"> Primary Address
    </label>
    <div class="col-md-2">
    <div class="form-check">
    <label class="form-check-label">
    <input class="form-check-input" id="primary1Idi" type="radio" value="homei"
    formControlName="IS_HEAD_OFFICE"> Home
    </label>
    </div>
    </div>
    </div>





    share|improve this answer

























    • Hi jose what you shared is a simple scenario. I tried providing different values. Not sure if the way i have done is right. Please find the updated code. However it is not yet working for me

      – Tom
      Mar 10 at 0:05











    • Hi @Tom I noticed another issue, your radio buttons are assigned to different forms. If you take close attention to your ngFor you're setting a different formGroupName on each iteration, and on each iteration defining the radio buttons. Since they're inside elements with different formGroupName, the reactive forms directive from angular treats them as different elements and so the value isn't toggling as expected. Try setting the same formGroupName on the form that contains the addresses. I'll update my original answer with this

      – José Gazzano
      Mar 10 at 13:39











    • Hey Jose, I have updated the html bit on the post. I have given the same formGroupName as Test but that doesnt seem to make any difference

      – Tom
      Mar 12 at 14:56











    • Were you able to spot the issue ?

      – Tom
      Mar 15 at 10:55











    • Hey @Tom I couldn't get to replicate that. But I'll try again if I manage to get some spare time. Good luck!

      – José Gazzano
      Mar 15 at 15:48













    3












    3








    3







    the problem here is that your radio buttons have the same value assigned, so they are treated as the same input. That's why you can't toggle between them.



    Try setting a different value for each of them and it should be working as expected and this will also help with your form logic and validations ;)



    As a reminder, the only thing you need in order to make a group of radio buttons to toggle between them is setting the same name (or formControlName in your case) to all of them, and of course also different values. Do not set the same Id as it was previously suggested because that's not semantically correct from an HTML perspective



    Here's a minimal StackBlitz example where you can try it out https://stackblitz.com/edit/angular-lvahzg?file=src%2Fapp%2Fapp.component.ts



    It has all the radio buttons with the same formContrlName and the same value, try setting different values to each of them and see how it works ;)



    Edit: Adding Angular documentation regarding radio buttons withing reactive forms https://angular.io/api/forms/RadioControlValueAccessor



    Edit 2: Here's the issue I was talking on my comment



    <div formArrayName="Addresses" *ngFor="let item of frmFirm.get('Addresses').controls; let i = index;">

    <div [formGroupName]="i"> --> This formGroupName should be the same on each loop in order to make the radio buttons to toggle between them

    <div class="form-group row">
    <label class="col-md-2 col-form-label"> Primary Address
    </label>
    <div class="col-md-2">
    <div class="form-check">
    <label class="form-check-label">
    <input class="form-check-input" id="primary1Idi" type="radio" value="homei"
    formControlName="IS_HEAD_OFFICE"> Home
    </label>
    </div>
    </div>
    </div>





    share|improve this answer















    the problem here is that your radio buttons have the same value assigned, so they are treated as the same input. That's why you can't toggle between them.



    Try setting a different value for each of them and it should be working as expected and this will also help with your form logic and validations ;)



    As a reminder, the only thing you need in order to make a group of radio buttons to toggle between them is setting the same name (or formControlName in your case) to all of them, and of course also different values. Do not set the same Id as it was previously suggested because that's not semantically correct from an HTML perspective



    Here's a minimal StackBlitz example where you can try it out https://stackblitz.com/edit/angular-lvahzg?file=src%2Fapp%2Fapp.component.ts



    It has all the radio buttons with the same formContrlName and the same value, try setting different values to each of them and see how it works ;)



    Edit: Adding Angular documentation regarding radio buttons withing reactive forms https://angular.io/api/forms/RadioControlValueAccessor



    Edit 2: Here's the issue I was talking on my comment



    <div formArrayName="Addresses" *ngFor="let item of frmFirm.get('Addresses').controls; let i = index;">

    <div [formGroupName]="i"> --> This formGroupName should be the same on each loop in order to make the radio buttons to toggle between them

    <div class="form-group row">
    <label class="col-md-2 col-form-label"> Primary Address
    </label>
    <div class="col-md-2">
    <div class="form-check">
    <label class="form-check-label">
    <input class="form-check-input" id="primary1Idi" type="radio" value="homei"
    formControlName="IS_HEAD_OFFICE"> Home
    </label>
    </div>
    </div>
    </div>






    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Mar 10 at 13:41

























    answered Mar 9 at 16:24









    José GazzanoJosé Gazzano

    1124




    1124












    • Hi jose what you shared is a simple scenario. I tried providing different values. Not sure if the way i have done is right. Please find the updated code. However it is not yet working for me

      – Tom
      Mar 10 at 0:05











    • Hi @Tom I noticed another issue, your radio buttons are assigned to different forms. If you take close attention to your ngFor you're setting a different formGroupName on each iteration, and on each iteration defining the radio buttons. Since they're inside elements with different formGroupName, the reactive forms directive from angular treats them as different elements and so the value isn't toggling as expected. Try setting the same formGroupName on the form that contains the addresses. I'll update my original answer with this

      – José Gazzano
      Mar 10 at 13:39











    • Hey Jose, I have updated the html bit on the post. I have given the same formGroupName as Test but that doesnt seem to make any difference

      – Tom
      Mar 12 at 14:56











    • Were you able to spot the issue ?

      – Tom
      Mar 15 at 10:55











    • Hey @Tom I couldn't get to replicate that. But I'll try again if I manage to get some spare time. Good luck!

      – José Gazzano
      Mar 15 at 15:48

















    • Hi jose what you shared is a simple scenario. I tried providing different values. Not sure if the way i have done is right. Please find the updated code. However it is not yet working for me

      – Tom
      Mar 10 at 0:05











    • Hi @Tom I noticed another issue, your radio buttons are assigned to different forms. If you take close attention to your ngFor you're setting a different formGroupName on each iteration, and on each iteration defining the radio buttons. Since they're inside elements with different formGroupName, the reactive forms directive from angular treats them as different elements and so the value isn't toggling as expected. Try setting the same formGroupName on the form that contains the addresses. I'll update my original answer with this

      – José Gazzano
      Mar 10 at 13:39











    • Hey Jose, I have updated the html bit on the post. I have given the same formGroupName as Test but that doesnt seem to make any difference

      – Tom
      Mar 12 at 14:56











    • Were you able to spot the issue ?

      – Tom
      Mar 15 at 10:55











    • Hey @Tom I couldn't get to replicate that. But I'll try again if I manage to get some spare time. Good luck!

      – José Gazzano
      Mar 15 at 15:48
















    Hi jose what you shared is a simple scenario. I tried providing different values. Not sure if the way i have done is right. Please find the updated code. However it is not yet working for me

    – Tom
    Mar 10 at 0:05





    Hi jose what you shared is a simple scenario. I tried providing different values. Not sure if the way i have done is right. Please find the updated code. However it is not yet working for me

    – Tom
    Mar 10 at 0:05













    Hi @Tom I noticed another issue, your radio buttons are assigned to different forms. If you take close attention to your ngFor you're setting a different formGroupName on each iteration, and on each iteration defining the radio buttons. Since they're inside elements with different formGroupName, the reactive forms directive from angular treats them as different elements and so the value isn't toggling as expected. Try setting the same formGroupName on the form that contains the addresses. I'll update my original answer with this

    – José Gazzano
    Mar 10 at 13:39





    Hi @Tom I noticed another issue, your radio buttons are assigned to different forms. If you take close attention to your ngFor you're setting a different formGroupName on each iteration, and on each iteration defining the radio buttons. Since they're inside elements with different formGroupName, the reactive forms directive from angular treats them as different elements and so the value isn't toggling as expected. Try setting the same formGroupName on the form that contains the addresses. I'll update my original answer with this

    – José Gazzano
    Mar 10 at 13:39













    Hey Jose, I have updated the html bit on the post. I have given the same formGroupName as Test but that doesnt seem to make any difference

    – Tom
    Mar 12 at 14:56





    Hey Jose, I have updated the html bit on the post. I have given the same formGroupName as Test but that doesnt seem to make any difference

    – Tom
    Mar 12 at 14:56













    Were you able to spot the issue ?

    – Tom
    Mar 15 at 10:55





    Were you able to spot the issue ?

    – Tom
    Mar 15 at 10:55













    Hey @Tom I couldn't get to replicate that. But I'll try again if I manage to get some spare time. Good luck!

    – José Gazzano
    Mar 15 at 15:48





    Hey @Tom I couldn't get to replicate that. But I'll try again if I manage to get some spare time. Good luck!

    – José Gazzano
    Mar 15 at 15:48











    0














    My understanding of your requirements is when a user sets an address as "Home" all of the other addresses should be unset as "Home." If my understanding is correct, You were on the right track with your original code but there are a couple of things that you will need to correct.



    Form Group Name



    With the ngFor you are creating an address for each control that is in the FormArray. Each control in the FormArray is a FormGroup whos name is its index in the FormArray. So you were correct when you had i has the formGroupName



    So change back to this



    <div [formGroupName]="i">


    Create an OnPrimaryToggleMethod



    Add a method to the component that will set IS_HEAD_OFFICE to false for every other control in the array



    onPrimaryToggled(newValue: any, changedItem: FormControl) 
    if (newValue.returnValue === true)
    const toChange = this.frmFirm.get('Addresses').controls.filter(el => el !== changedItem);
    toChange.forEach(el => el.patchValue(
    IS_HEAD_OFFICE: false
    ));




    Update the checkbox to call the toggle method



    Add a change event binding to the the radio button to call the method



    (change)="onPrimaryToggled($event, item)"


    Now it should look like the below code (btw, you don't need the value attribute)



    <div formArrayName="Addresses" *ngFor="let item of frmFirm.get('Addresses').controls; let i = index;">
    <div [formGroupName]="i">
    <div class="form-group row">
    <label class="col-md-2 col-form-label"> Primary Address </label>
    <div class="col-md-2">
    <div class="form-check">
    <label class="form-check-label">

    <input
    class="form-check-input"
    id="primary1Idi"
    type="radio"
    formControlName="IS_HEAD_OFFICE"
    (change)="OnPrimaryToggled($event, item)"
    > Home

    </label>
    </div>
    </div>
    </div>
    </div>
    </div>





    share|improve this answer



























      0














      My understanding of your requirements is when a user sets an address as "Home" all of the other addresses should be unset as "Home." If my understanding is correct, You were on the right track with your original code but there are a couple of things that you will need to correct.



      Form Group Name



      With the ngFor you are creating an address for each control that is in the FormArray. Each control in the FormArray is a FormGroup whos name is its index in the FormArray. So you were correct when you had i has the formGroupName



      So change back to this



      <div [formGroupName]="i">


      Create an OnPrimaryToggleMethod



      Add a method to the component that will set IS_HEAD_OFFICE to false for every other control in the array



      onPrimaryToggled(newValue: any, changedItem: FormControl) 
      if (newValue.returnValue === true)
      const toChange = this.frmFirm.get('Addresses').controls.filter(el => el !== changedItem);
      toChange.forEach(el => el.patchValue(
      IS_HEAD_OFFICE: false
      ));




      Update the checkbox to call the toggle method



      Add a change event binding to the the radio button to call the method



      (change)="onPrimaryToggled($event, item)"


      Now it should look like the below code (btw, you don't need the value attribute)



      <div formArrayName="Addresses" *ngFor="let item of frmFirm.get('Addresses').controls; let i = index;">
      <div [formGroupName]="i">
      <div class="form-group row">
      <label class="col-md-2 col-form-label"> Primary Address </label>
      <div class="col-md-2">
      <div class="form-check">
      <label class="form-check-label">

      <input
      class="form-check-input"
      id="primary1Idi"
      type="radio"
      formControlName="IS_HEAD_OFFICE"
      (change)="OnPrimaryToggled($event, item)"
      > Home

      </label>
      </div>
      </div>
      </div>
      </div>
      </div>





      share|improve this answer

























        0












        0








        0







        My understanding of your requirements is when a user sets an address as "Home" all of the other addresses should be unset as "Home." If my understanding is correct, You were on the right track with your original code but there are a couple of things that you will need to correct.



        Form Group Name



        With the ngFor you are creating an address for each control that is in the FormArray. Each control in the FormArray is a FormGroup whos name is its index in the FormArray. So you were correct when you had i has the formGroupName



        So change back to this



        <div [formGroupName]="i">


        Create an OnPrimaryToggleMethod



        Add a method to the component that will set IS_HEAD_OFFICE to false for every other control in the array



        onPrimaryToggled(newValue: any, changedItem: FormControl) 
        if (newValue.returnValue === true)
        const toChange = this.frmFirm.get('Addresses').controls.filter(el => el !== changedItem);
        toChange.forEach(el => el.patchValue(
        IS_HEAD_OFFICE: false
        ));




        Update the checkbox to call the toggle method



        Add a change event binding to the the radio button to call the method



        (change)="onPrimaryToggled($event, item)"


        Now it should look like the below code (btw, you don't need the value attribute)



        <div formArrayName="Addresses" *ngFor="let item of frmFirm.get('Addresses').controls; let i = index;">
        <div [formGroupName]="i">
        <div class="form-group row">
        <label class="col-md-2 col-form-label"> Primary Address </label>
        <div class="col-md-2">
        <div class="form-check">
        <label class="form-check-label">

        <input
        class="form-check-input"
        id="primary1Idi"
        type="radio"
        formControlName="IS_HEAD_OFFICE"
        (change)="OnPrimaryToggled($event, item)"
        > Home

        </label>
        </div>
        </div>
        </div>
        </div>
        </div>





        share|improve this answer













        My understanding of your requirements is when a user sets an address as "Home" all of the other addresses should be unset as "Home." If my understanding is correct, You were on the right track with your original code but there are a couple of things that you will need to correct.



        Form Group Name



        With the ngFor you are creating an address for each control that is in the FormArray. Each control in the FormArray is a FormGroup whos name is its index in the FormArray. So you were correct when you had i has the formGroupName



        So change back to this



        <div [formGroupName]="i">


        Create an OnPrimaryToggleMethod



        Add a method to the component that will set IS_HEAD_OFFICE to false for every other control in the array



        onPrimaryToggled(newValue: any, changedItem: FormControl) 
        if (newValue.returnValue === true)
        const toChange = this.frmFirm.get('Addresses').controls.filter(el => el !== changedItem);
        toChange.forEach(el => el.patchValue(
        IS_HEAD_OFFICE: false
        ));




        Update the checkbox to call the toggle method



        Add a change event binding to the the radio button to call the method



        (change)="onPrimaryToggled($event, item)"


        Now it should look like the below code (btw, you don't need the value attribute)



        <div formArrayName="Addresses" *ngFor="let item of frmFirm.get('Addresses').controls; let i = index;">
        <div [formGroupName]="i">
        <div class="form-group row">
        <label class="col-md-2 col-form-label"> Primary Address </label>
        <div class="col-md-2">
        <div class="form-check">
        <label class="form-check-label">

        <input
        class="form-check-input"
        id="primary1Idi"
        type="radio"
        formControlName="IS_HEAD_OFFICE"
        (change)="OnPrimaryToggled($event, item)"
        > Home

        </label>
        </div>
        </div>
        </div>
        </div>
        </div>






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Mar 15 at 22:18









        William StevensWilliam Stevens

        2113




        2113



























            draft saved

            draft discarded
















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid


            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.

            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55044248%2fradio-button-toggling-not-working-in-reactive-form%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            1928 у кіно

            Захаров Федір Захарович

            Ель Греко