@@ -75,7 +75,6 @@ export class MdSlideToggle extends _MdSlideToggleMixinBase
75
75
76
76
// A unique id for the slide-toggle. By default the id is auto-generated.
77
77
private _uniqueId = `md-slide-toggle-${ ++ nextId } ` ;
78
- private _checked : boolean = false ;
79
78
private _slideRenderer : SlideToggleRenderer = null ;
80
79
private _required : boolean = false ;
81
80
private _disableRipple : boolean = false ;
@@ -95,6 +94,9 @@ export class MdSlideToggle extends _MdSlideToggleMixinBase
95
94
/** Whether the label should appear after or before the slide-toggle. Defaults to 'after' */
96
95
@Input ( ) labelPosition : 'before' | 'after' = 'after' ;
97
96
97
+ /** Whether the slide-toggle element is checked or not */
98
+ @Input ( ) checked : boolean = false ;
99
+
98
100
/** Used to set the aria-label attribute on the underlying input element. */
99
101
@Input ( 'aria-label' ) ariaLabel : string = null ;
100
102
@@ -144,29 +146,30 @@ export class MdSlideToggle extends _MdSlideToggleMixinBase
144
146
}
145
147
146
148
/**
147
- * The onChangeEvent method will be also called on click.
148
- * This is because everything for the slide-toggle is wrapped inside of a label,
149
- * which triggers a onChange event on click.
149
+ * This function will called if the underlying input changed its value through user interaction.
150
150
*/
151
151
_onChangeEvent ( event : Event ) {
152
152
// We always have to stop propagation on the change event.
153
153
// Otherwise the change event, from the input element, will bubble up and
154
154
// emit its event object to the component's `change` output.
155
155
event . stopPropagation ( ) ;
156
156
157
- // Once a drag is currently in progress, we do not want to toggle the slide-toggle on a click.
158
- if ( ! this . disabled && ! this . _slideRenderer . dragging ) {
159
- this . toggle ( ) ;
157
+ // Sync the value from the underlying input element with the slide-toggle component.
158
+ this . checked = this . _inputElement . nativeElement . checked ;
160
159
161
- // Emit our custom change event if the native input emitted one.
162
- // It is important to only emit it, if the native input triggered one, because
163
- // we don't want to trigger a change event, when the `checked` variable changes for example.
164
- this . _emitChangeEvent ( ) ;
165
- }
160
+ // Emit our custom change event if the native input emitted one.
161
+ // It is important to only emit it, if the native input triggered one, because we don't want
162
+ // to trigger a change event, when the `checked` variable changes programmatically.
163
+ this . _emitChangeEvent ( ) ;
166
164
}
167
165
168
166
_onInputClick ( event : Event ) {
169
- this . onTouched ( ) ;
167
+ // In some situations the user will release the mouse on the label element. The label element
168
+ // redirects the click to the underlying input element and will result in a value change.
169
+ // Prevent the default behavior if dragging, because the value will be set after drag.
170
+ if ( this . _slideRenderer . dragging ) {
171
+ event . preventDefault ( ) ;
172
+ }
170
173
171
174
// We have to stop propagation for click events on the visual hidden input element.
172
175
// By default, when a user clicks on a label element, a generated click event will be
@@ -204,16 +207,6 @@ export class MdSlideToggle extends _MdSlideToggleMixinBase
204
207
this . _focusOriginMonitor . focusVia ( this . _inputElement . nativeElement , 'keyboard' ) ;
205
208
}
206
209
207
- /** Whether the slide-toggle is checked. */
208
- @Input ( )
209
- get checked ( ) { return ! ! this . _checked ; }
210
- set checked ( value ) {
211
- if ( this . checked !== ! ! value ) {
212
- this . _checked = value ;
213
- this . onChange ( this . _checked ) ;
214
- }
215
- }
216
-
217
210
/** Toggles the checked state of the slide-toggle. */
218
211
toggle ( ) {
219
212
this . checked = ! this . checked ;
@@ -235,15 +228,17 @@ export class MdSlideToggle extends _MdSlideToggleMixinBase
235
228
}
236
229
}
237
230
238
- /** Emits the change event to the `change` output EventEmitter */
231
+ /**
232
+ * Emits a change event on the `change` output. Also notifies the FormControl about the change.
233
+ */
239
234
private _emitChangeEvent ( ) {
240
235
let event = new MdSlideToggleChange ( ) ;
241
236
event . source = this ;
242
237
event . checked = this . checked ;
243
238
this . change . emit ( event ) ;
239
+ this . onChange ( this . checked ) ;
244
240
}
245
241
246
-
247
242
_onDragStart ( ) {
248
243
if ( ! this . disabled ) {
249
244
this . _slideRenderer . startThumbDrag ( this . checked ) ;
0 commit comments