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