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