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