@@ -70,7 +70,6 @@ export class MdSlideToggle extends _MdSlideToggleMixinBase
70
70
71
71
// A unique id for the slide-toggle. By default the id is auto-generated.
72
72
private _uniqueId = `md-slide-toggle-${ ++ nextId } ` ;
73
- private _checked : boolean = false ;
74
73
private _color : string ;
75
74
private _slideRenderer : SlideToggleRenderer = null ;
76
75
private _required : boolean = false ;
@@ -91,6 +90,9 @@ export class MdSlideToggle extends _MdSlideToggleMixinBase
91
90
/** Whether the label should appear after or before the slide-toggle. Defaults to 'after' */
92
91
@Input ( ) labelPosition : 'before' | 'after' = 'after' ;
93
92
93
+ /** Whether the slide-toggle element is checked or not */
94
+ @Input ( ) checked : boolean = false ;
95
+
94
96
/** Used to set the aria-label attribute on the underlying input element. */
95
97
@Input ( 'aria-label' ) ariaLabel : string = null ;
96
98
@@ -138,29 +140,30 @@ export class MdSlideToggle extends _MdSlideToggleMixinBase
138
140
}
139
141
140
142
/**
141
- * The onChangeEvent method will be also called on click.
142
- * This is because everything for the slide-toggle is wrapped inside of a label,
143
- * which triggers a onChange event on click.
143
+ * This function will called if the underlying input changed its value through user interaction.
144
144
*/
145
145
_onChangeEvent ( event : Event ) {
146
146
// We always have to stop propagation on the change event.
147
147
// Otherwise the change event, from the input element, will bubble up and
148
148
// emit its event object to the component's `change` output.
149
149
event . stopPropagation ( ) ;
150
150
151
- // Once a drag is currently in progress, we do not want to toggle the slide-toggle on a click.
152
- if ( ! this . disabled && ! this . _slideRenderer . dragging ) {
153
- this . toggle ( ) ;
151
+ // Sync the value from the underlying input element with the slide-toggle component.
152
+ this . checked = this . _inputElement . nativeElement . checked ;
154
153
155
- // Emit our custom change event if the native input emitted one.
156
- // It is important to only emit it, if the native input triggered one, because
157
- // we don't want to trigger a change event, when the `checked` variable changes for example.
158
- this . _emitChangeEvent ( ) ;
159
- }
154
+ // Emit our custom change event if the native input emitted one.
155
+ // It is important to only emit it, if the native input triggered one, because we don't want
156
+ // to trigger a change event, when the `checked` variable changes programmatically.
157
+ this . _emitChangeEvent ( ) ;
160
158
}
161
159
162
160
_onInputClick ( event : Event ) {
163
- this . onTouched ( ) ;
161
+ // In some situations the user will release the mouse on the label element. The label element
162
+ // redirects the click to the underlying input element and will result in a value change.
163
+ // Prevent the default behavior if dragging, because the value will be set after drag.
164
+ if ( this . _slideRenderer . dragging ) {
165
+ event . preventDefault ( ) ;
166
+ }
164
167
165
168
// We have to stop propagation for click events on the visual hidden input element.
166
169
// By default, when a user clicks on a label element, a generated click event will be
@@ -197,16 +200,6 @@ export class MdSlideToggle extends _MdSlideToggleMixinBase
197
200
this . _focusOriginMonitor . focusVia ( this . _inputElement . nativeElement , this . _renderer , 'keyboard' ) ;
198
201
}
199
202
200
- /** Whether the slide-toggle is checked. */
201
- @Input ( )
202
- get checked ( ) { return ! ! this . _checked ; }
203
- set checked ( value ) {
204
- if ( this . checked !== ! ! value ) {
205
- this . _checked = value ;
206
- this . onChange ( this . _checked ) ;
207
- }
208
- }
209
-
210
203
/** The color of the slide-toggle. Can be primary, accent, or warn. */
211
204
@Input ( )
212
205
get color ( ) : string { return this . _color ; }
@@ -251,12 +244,15 @@ export class MdSlideToggle extends _MdSlideToggleMixinBase
251
244
}
252
245
}
253
246
254
- /** Emits the change event to the `change` output EventEmitter */
247
+ /**
248
+ * Emits a change event on the `change` output. Also notifies the FormControl about the change.
249
+ */
255
250
private _emitChangeEvent ( ) {
256
251
let event = new MdSlideToggleChange ( ) ;
257
252
event . source = this ;
258
253
event . checked = this . checked ;
259
254
this . change . emit ( event ) ;
255
+ this . onChange ( this . checked ) ;
260
256
}
261
257
262
258
0 commit comments