@@ -127,6 +127,13 @@ export class MdRadioGroup implements AfterContentInit, ControlValueAccessor {
127127 this . _value = newValue ;
128128
129129 this . _updateSelectedRadioFromValue ( ) ;
130+ this . _checkSelectedRadioButton ( ) ;
131+ }
132+ }
133+
134+ _checkSelectedRadioButton ( ) {
135+ if ( this . selected && ! this . _selected . checked ) {
136+ this . _selected . checked = true ;
130137 }
131138 }
132139
@@ -139,9 +146,7 @@ export class MdRadioGroup implements AfterContentInit, ControlValueAccessor {
139146 this . _selected = selected ;
140147 this . value = selected ? selected . value : null ;
141148
142- if ( selected && ! selected . checked ) {
143- selected . checked = true ;
144- }
149+ this . _checkSelectedRadioButton ( ) ;
145150 }
146151
147152 /**
@@ -180,14 +185,13 @@ export class MdRadioGroup implements AfterContentInit, ControlValueAccessor {
180185 let isAlreadySelected = this . _selected != null && this . _selected . value == this . _value ;
181186
182187 if ( this . _radios != null && ! isAlreadySelected ) {
183- let matchingRadio = this . _radios . filter ( radio => radio . value == this . _value ) [ 0 ] ;
184-
185- if ( matchingRadio ) {
186- this . selected = matchingRadio ;
187- } else if ( this . value == null ) {
188- this . selected = null ;
189- this . _radios . forEach ( radio => { radio . checked = false ; } ) ;
190- }
188+ this . _selected = null ;
189+ this . _radios . forEach ( radio => {
190+ radio . checked = this . value == radio . value ;
191+ if ( radio . checked ) {
192+ this . _selected = radio ;
193+ }
194+ } ) ;
191195 }
192196 }
193197
@@ -303,19 +307,21 @@ export class MdRadioButton implements OnInit {
303307 }
304308
305309 set checked ( newCheckedState : boolean ) {
306- this . _checked = newCheckedState ;
307-
308- if ( newCheckedState && this . radioGroup && this . radioGroup . value != this . value ) {
309- this . radioGroup . selected = this ;
310- } else if ( ! newCheckedState && this . radioGroup && this . radioGroup . value == this . value ) {
311- // When unchecking the selected radio button, update the selected radio
312- // property on the group.
313- this . radioGroup . selected = null ;
314- }
310+ if ( this . _checked != newCheckedState ) {
311+ this . _checked = newCheckedState ;
312+
313+ if ( newCheckedState && this . radioGroup && this . radioGroup . value != this . value ) {
314+ this . radioGroup . selected = this ;
315+ } else if ( ! newCheckedState && this . radioGroup && this . radioGroup . value == this . value ) {
316+ // When unchecking the selected radio button, update the selected radio
317+ // property on the group.
318+ this . radioGroup . selected = null ;
319+ }
315320
316- if ( newCheckedState ) {
317- // Notify all radio buttons with the same name to un-check.
318- this . radioDispatcher . notify ( this . id , this . name ) ;
321+ if ( newCheckedState ) {
322+ // Notify all radio buttons with the same name to un-check.
323+ this . radioDispatcher . notify ( this . id , this . name ) ;
324+ }
319325 }
320326 }
321327
@@ -327,10 +333,17 @@ export class MdRadioButton implements OnInit {
327333
328334 set value ( value : any ) {
329335 if ( this . _value != value ) {
330- if ( this . radioGroup != null && this . checked ) {
331- this . radioGroup . value = value ;
332- }
333336 this . _value = value ;
337+ if ( this . radioGroup != null ) {
338+ if ( ! this . checked ) {
339+ // Update checked when the value changed to match the radio group's value
340+ this . checked = this . radioGroup . value == value ;
341+ }
342+ if ( this . checked ) {
343+ this . radioGroup . selected = this ;
344+ }
345+ }
346+
334347 }
335348 }
336349
0 commit comments