@@ -69,7 +69,7 @@ export class MdRadioGroup implements AfterContentInit, ControlValueAccessor {
69
69
private _value : any = null ;
70
70
71
71
/** The HTML name attribute applied to radio buttons in this group. */
72
- private _name : string = null ;
72
+ private _name : string = `md-radio-group- ${ _uniqueIdCounter ++ } ` ;
73
73
74
74
/** Disables all individual radio buttons assigned to this group. */
75
75
private _disabled : boolean = false ;
@@ -101,14 +101,7 @@ export class MdRadioGroup implements AfterContentInit, ControlValueAccessor {
101
101
102
102
set name ( value : string ) {
103
103
this . _name = value ;
104
- this . _updateChildRadioNames ( ) ;
105
- }
106
-
107
- /** Propagate name attribute to radio buttons. */
108
- private _updateChildRadioNames ( ) : void {
109
- ( this . _radios || [ ] ) . forEach ( radio => {
110
- radio . name = this . _name ;
111
- } ) ;
104
+ this . _updateRadioButtonNames ( ) ;
112
105
}
113
106
114
107
@Input ( )
@@ -160,10 +153,6 @@ export class MdRadioGroup implements AfterContentInit, ControlValueAccessor {
160
153
* @internal
161
154
*/
162
155
ngAfterContentInit ( ) {
163
- if ( ! this . _name ) {
164
- this . name = `md-radio-group-${ _uniqueIdCounter ++ } ` ;
165
- }
166
-
167
156
// Mark this component as initialized in AfterContentInit because the initial value can
168
157
// possibly be set by NgModel on MdRadioGroup, and it is possible that the OnInit of the
169
158
// NgModel occurs *after* the OnInit of the MdRadioGroup.
@@ -181,9 +170,15 @@ export class MdRadioGroup implements AfterContentInit, ControlValueAccessor {
181
170
}
182
171
}
183
172
173
+ private _updateRadioButtonNames ( ) : void {
174
+ ( this . _radios || [ ] ) . forEach ( radio => {
175
+ radio . name = this . name ;
176
+ } ) ;
177
+ }
178
+
184
179
/** Updates the `selected` radio button from the internal _value state. */
185
180
private _updateSelectedRadioFromValue ( ) : void {
186
- // If the value already matches the selected radio, no dothing .
181
+ // If the value already matches the selected radio, do nothing .
187
182
let isAlreadySelected = this . _selected != null && this . _selected . value == this . _value ;
188
183
189
184
if ( this . _radios != null && ! isAlreadySelected ) {
@@ -192,8 +187,8 @@ export class MdRadioGroup implements AfterContentInit, ControlValueAccessor {
192
187
if ( matchingRadio ) {
193
188
this . selected = matchingRadio ;
194
189
} else if ( this . value == null ) {
195
- this . selected = null ;
196
- this . _radios . forEach ( radio => { radio . checked = false ; } ) ;
190
+ this . selected = null ;
191
+ this . _radios . forEach ( radio => { radio . checked = false ; } ) ;
197
192
}
198
193
}
199
194
}
@@ -206,7 +201,7 @@ export class MdRadioGroup implements AfterContentInit, ControlValueAccessor {
206
201
this . change . emit ( event ) ;
207
202
}
208
203
209
- /**
204
+ /**
210
205
* Implemented as part of ControlValueAccessor.
211
206
* @internal
212
207
*/
@@ -258,7 +253,7 @@ export class MdRadioButton implements OnInit {
258
253
/** The unique ID for the radio button. */
259
254
@HostBinding ( 'id' )
260
255
@Input ( )
261
- id : string ;
256
+ id : string = `md-radio- ${ _uniqueIdCounter ++ } ` ;
262
257
263
258
/** Analog to HTML 'name' attribute used to group radios for unique selection. */
264
259
@Input ( )
@@ -345,15 +340,11 @@ export class MdRadioButton implements OnInit {
345
340
346
341
/** @internal */
347
342
ngOnInit ( ) {
348
- // All radio buttons must have a unique id.
349
- if ( ! this . id ) {
350
- this . id = `md-radio-${ _uniqueIdCounter ++ } ` ;
351
- }
352
-
353
- // If the radio is inside of a radio group and it matches that group's value upon
354
- // initialization, start off as checked.
355
- if ( this . radioGroup && this . radioGroup . value === this . _value ) {
356
- this . checked = true ;
343
+ if ( this . radioGroup ) {
344
+ // If the radio is inside a radio group, determine if it should be checked
345
+ this . checked = this . radioGroup . value === this . _value ;
346
+ // Copy name from parent radio group
347
+ this . name = this . radioGroup . name ;
357
348
}
358
349
}
359
350
0 commit comments