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