Skip to content

Commit c86f227

Browse files
authored
Merge pull request #9163 from IgniteUI/simeonoff/fix-9155-11.1.x
fix(radio): ngModelChange fires multiple times
2 parents 0f2b508 + 6c42a2c commit c86f227

File tree

3 files changed

+24
-10
lines changed

3 files changed

+24
-10
lines changed

projects/igniteui-angular/src/lib/radio/radio.component.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -349,9 +349,11 @@ export class IgxRadioComponent implements ControlValueAccessor, EditorProvider {
349349
this.nativeRadio.nativeElement.blur();
350350
}
351351

352-
this.checked = true;
353-
this.change.emit({ value: this.value, radio: this });
354-
this._onChangeCallback(this.value);
352+
if(!this.checked) {
353+
this.checked = true;
354+
this.change.emit({ value: this.value, radio: this });
355+
this._onChangeCallback(this.value);
356+
}
355357
}
356358

357359
/**

src/app/input/input.sample.html

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -118,8 +118,10 @@ <h4 class="sample-title">Checkbox sample</h4>
118118
<article class="sample-column">
119119
<h4 class="sample-title">Radio w/ ngModel</h4>
120120

121-
<igx-radio [ngModel]="selected" (change)="onChange($event)" value="option1">Option 1</igx-radio>
122-
<igx-radio [ngModel]="selected" (change)="onChange($event)" value="option2">Option 2</igx-radio>
123-
<igx-radio [ngModel]="selected" (change)="onChange($event)" value="option3" disabled="true">Option 3</igx-radio>
121+
<igx-radio [(ngModel)]="selected" (ngModelChange)="onChange($event)" (change)="onRadioChanged($event)" value="option1">Option 1</igx-radio>
122+
<igx-radio [(ngModel)]="selected" (ngModelChange)="onChange($event)" (change)="onRadioChanged($event)" value="option2">Option 2</igx-radio>
123+
<igx-radio [(ngModel)]="selected" (ngModelChange)="onChange($event)" (change)="onRadioChanged($event)" value="option3" disabled="true">Option 3</igx-radio>
124+
<br>
125+
<button igxButton="raised" (click)="selectSecond()">Select second</button>
124126
</article>
125127
</section>

src/app/input/input.sample.ts

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
1-
import { Component } from '@angular/core';
1+
import { Component, ChangeDetectionStrategy } from '@angular/core';
2+
import { IChangeRadioEventArgs } from 'igniteui-angular';
23

34
@Component({
5+
changeDetection: ChangeDetectionStrategy.OnPush,
46
selector: 'app-input-sample',
57
styleUrls: ['input.sample.css'],
68
templateUrl: 'input.sample.html'
79
})
810
export class InputSampleComponent {
911
public placeholder = 'Please enter a value';
10-
public selected: string;
12+
public selected = 'option1';
1113

1214
public user = {
1315
comment: '',
@@ -47,12 +49,20 @@ export class InputSampleComponent {
4749
disabled: true
4850
}];
4951

50-
public onClick(event) {
52+
public onClick(event: MouseEvent) {
5153
console.log(event);
5254
}
5355

54-
public onChange(event) {
56+
public onChange(value: string) {
57+
console.log(value);
58+
}
59+
60+
public onRadioChanged(event: IChangeRadioEventArgs) {
5561
console.log(event);
5662
this.selected = event.value;
5763
}
64+
65+
public selectSecond() {
66+
this.selected = 'option2';
67+
}
5868
}

0 commit comments

Comments
 (0)