@@ -81,7 +81,6 @@ export class MdSlideToggle extends _MdSlideToggleMixinBase
81
81
private onTouched = ( ) => { } ;
82
82
83
83
private _uniqueId : string = `md-slide-toggle-${ ++ nextUniqueId } ` ;
84
- private _checked : boolean = false ;
85
84
private _slideRenderer : SlideToggleRenderer ;
86
85
private _required : boolean = false ;
87
86
private _disableRipple : boolean = false ;
@@ -101,6 +100,9 @@ export class MdSlideToggle extends _MdSlideToggleMixinBase
101
100
/** Whether the label should appear after or before the slide-toggle. Defaults to 'after' */
102
101
@Input ( ) labelPosition : 'before' | 'after' = 'after' ;
103
102
103
+ /** Whether the slide-toggle element is checked or not */
104
+ @Input ( ) checked : boolean = false ;
105
+
104
106
/** Used to set the aria-label attribute on the underlying input element. */
105
107
@Input ( 'aria-label' ) ariaLabel : string | null = null ;
106
108
@@ -150,29 +152,30 @@ export class MdSlideToggle extends _MdSlideToggleMixinBase
150
152
}
151
153
152
154
/**
153
- * The onChangeEvent method will be also called on click.
154
- * This is because everything for the slide-toggle is wrapped inside of a label,
155
- * which triggers a onChange event on click.
155
+ * This function will called if the underlying input changed its value through user interaction.
156
156
*/
157
157
_onChangeEvent ( event : Event ) {
158
158
// We always have to stop propagation on the change event.
159
159
// Otherwise the change event, from the input element, will bubble up and
160
160
// emit its event object to the component's `change` output.
161
161
event . stopPropagation ( ) ;
162
162
163
- // Once a drag is currently in progress, we do not want to toggle the slide-toggle on a click.
164
- if ( ! this . disabled && ! this . _slideRenderer . dragging ) {
165
- this . toggle ( ) ;
163
+ // Sync the value from the underlying input element with the slide-toggle component.
164
+ this . checked = this . _inputElement . nativeElement . checked ;
166
165
167
- // Emit our custom change event if the native input emitted one.
168
- // It is important to only emit it, if the native input triggered one, because
169
- // we don't want to trigger a change event, when the `checked` variable changes for example.
170
- this . _emitChangeEvent ( ) ;
171
- }
166
+ // Emit our custom change event if the native input emitted one.
167
+ // It is important to only emit it, if the native input triggered one, because we don't want
168
+ // to trigger a change event, when the `checked` variable changes programmatically.
169
+ this . _emitChangeEvent ( ) ;
172
170
}
173
171
174
172
_onInputClick ( event : Event ) {
175
- this . onTouched ( ) ;
173
+ // In some situations the user will release the mouse on the label element. The label element
174
+ // redirects the click to the underlying input element and will result in a value change.
175
+ // Prevent the default behavior if dragging, because the value will be set after drag.
176
+ if ( this . _slideRenderer . dragging ) {
177
+ event . preventDefault ( ) ;
178
+ }
176
179
177
180
// We have to stop propagation for click events on the visual hidden input element.
178
181
// By default, when a user clicks on a label element, a generated click event will be
@@ -210,16 +213,6 @@ export class MdSlideToggle extends _MdSlideToggleMixinBase
210
213
this . _focusOriginMonitor . focusVia ( this . _inputElement . nativeElement , 'keyboard' ) ;
211
214
}
212
215
213
- /** Whether the slide-toggle is checked. */
214
- @Input ( )
215
- get checked ( ) { return ! ! this . _checked ; }
216
- set checked ( value ) {
217
- if ( this . checked !== ! ! value ) {
218
- this . _checked = value ;
219
- this . onChange ( this . _checked ) ;
220
- }
221
- }
222
-
223
216
/** Toggles the checked state of the slide-toggle. */
224
217
toggle ( ) {
225
218
this . checked = ! this . checked ;
@@ -241,15 +234,17 @@ export class MdSlideToggle extends _MdSlideToggleMixinBase
241
234
}
242
235
}
243
236
244
- /** Emits the change event to the `change` output EventEmitter */
237
+ /**
238
+ * Emits a change event on the `change` output. Also notifies the FormControl about the change.
239
+ */
245
240
private _emitChangeEvent ( ) {
246
241
let event = new MdSlideToggleChange ( ) ;
247
242
event . source = this ;
248
243
event . checked = this . checked ;
249
244
this . change . emit ( event ) ;
245
+ this . onChange ( this . checked ) ;
250
246
}
251
247
252
-
253
248
_onDragStart ( ) {
254
249
if ( ! this . disabled ) {
255
250
this . _slideRenderer . startThumbDrag ( this . checked ) ;
0 commit comments