@@ -65,7 +65,6 @@ export class MdSlideToggle implements OnDestroy, AfterContentInit, ControlValueA
65
65
66
66
// A unique id for the slide-toggle. By default the id is auto-generated.
67
67
private _uniqueId = `md-slide-toggle-${ ++ nextId } ` ;
68
- private _checked : boolean = false ;
69
68
private _color : string ;
70
69
private _isMousedown : boolean = false ;
71
70
private _slideRenderer : SlideToggleRenderer = null ;
@@ -88,6 +87,9 @@ export class MdSlideToggle implements OnDestroy, AfterContentInit, ControlValueA
88
87
/** Whether the label should appear after or before the slide-toggle. Defaults to 'after' */
89
88
@Input ( ) labelPosition : 'before' | 'after' = 'after' ;
90
89
90
+ /** Whether the slide-toggle element is checked or not */
91
+ @Input ( ) checked : boolean = false ;
92
+
91
93
/** Used to set the aria-label attribute on the underlying input element. */
92
94
@Input ( 'aria-label' ) ariaLabel : string = null ;
93
95
@@ -139,29 +141,30 @@ export class MdSlideToggle implements OnDestroy, AfterContentInit, ControlValueA
139
141
}
140
142
141
143
/**
142
- * The onChangeEvent method will be also called on click.
143
- * This is because everything for the slide-toggle is wrapped inside of a label,
144
- * which triggers a onChange event on click.
144
+ * This function will called if the underlying input changed its value through user interaction.
145
145
*/
146
146
_onChangeEvent ( event : Event ) {
147
147
// We always have to stop propagation on the change event.
148
148
// Otherwise the change event, from the input element, will bubble up and
149
149
// emit its event object to the component's `change` output.
150
150
event . stopPropagation ( ) ;
151
151
152
- // Once a drag is currently in progress, we do not want to toggle the slide-toggle on a click.
153
- if ( ! this . disabled && ! this . _slideRenderer . dragging ) {
154
- this . toggle ( ) ;
152
+ // Sync the value from the underlying input element with the slide-toggle component.
153
+ this . checked = this . _inputElement . nativeElement . checked ;
155
154
156
- // Emit our custom change event if the native input emitted one.
157
- // It is important to only emit it, if the native input triggered one, because
158
- // we don't want to trigger a change event, when the `checked` variable changes for example.
159
- this . _emitChangeEvent ( ) ;
160
- }
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 we don't want
157
+ // to trigger a change event, when the `checked` variable changes programmatically.
158
+ this . _emitChangeEvent ( ) ;
161
159
}
162
160
163
161
_onInputClick ( event : Event ) {
164
- this . onTouched ( ) ;
162
+ // In some situations the user will release the mouse on the label element. The label element
163
+ // redirects the click to the underlying input element and will result in a value change.
164
+ // Prevent the default behavior if dragging, because the value will be set after drag.
165
+ if ( this . _slideRenderer . dragging ) {
166
+ event . preventDefault ( ) ;
167
+ }
165
168
166
169
// We have to stop propagation for click events on the visual hidden input element.
167
170
// By default, when a user clicks on a label element, a generated click event will be
@@ -207,16 +210,6 @@ export class MdSlideToggle implements OnDestroy, AfterContentInit, ControlValueA
207
210
this . _focusOriginMonitor . focusVia ( this . _inputElement . nativeElement , this . _renderer , '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
/** The color of the slide-toggle. Can be primary, accent, or warn. */
221
214
@Input ( )
222
215
get color ( ) : string { return this . _color ; }
@@ -257,12 +250,15 @@ export class MdSlideToggle implements OnDestroy, AfterContentInit, ControlValueA
257
250
}
258
251
}
259
252
260
- /** Emits the change event to the `change` output EventEmitter */
253
+ /**
254
+ * Emits a change event on the `change` output. Also notifies the FormControl about the change.
255
+ */
261
256
private _emitChangeEvent ( ) {
262
257
let event = new MdSlideToggleChange ( ) ;
263
258
event . source = this ;
264
259
event . checked = this . checked ;
265
260
this . _change . emit ( event ) ;
261
+ this . onChange ( this . checked ) ;
266
262
}
267
263
268
264
0 commit comments