@@ -16,7 +16,9 @@ import {
16
16
Optional ,
17
17
Output ,
18
18
Renderer2 ,
19
- ViewEncapsulation
19
+ ViewEncapsulation ,
20
+ ChangeDetectionStrategy ,
21
+ ChangeDetectorRef ,
20
22
} from '@angular/core' ;
21
23
import { ControlValueAccessor , NG_VALUE_ACCESSOR } from '@angular/forms' ;
22
24
import { coerceBooleanProperty , coerceNumberProperty , HammerInput } from '../core' ;
@@ -118,31 +120,33 @@ export const _MdSliderMixinBase = mixinDisabled(MdSliderBase);
118
120
styleUrls : [ 'slider.css' ] ,
119
121
inputs : [ 'disabled' ] ,
120
122
encapsulation : ViewEncapsulation . None ,
123
+ changeDetection : ChangeDetectionStrategy . OnPush ,
121
124
} )
122
125
export class MdSlider extends _MdSliderMixinBase
123
126
implements ControlValueAccessor , OnDestroy , CanDisable {
124
127
/** Whether the slider is inverted. */
125
128
@Input ( )
126
129
get invert ( ) { return this . _invert ; }
127
- set invert ( value : any ) { this . _invert = coerceBooleanProperty ( value ) ; }
130
+ set invert ( value : any ) {
131
+ this . _invert = coerceBooleanProperty ( value ) ;
132
+ }
128
133
private _invert = false ;
129
134
130
135
/** The maximum value that the slider can have. */
131
136
@Input ( )
132
- get max ( ) {
133
- return this . _max ;
134
- }
137
+ get max ( ) { return this . _max ; }
135
138
set max ( v : number ) {
136
139
this . _max = coerceNumberProperty ( v , this . _max ) ;
137
140
this . _percent = this . _calculatePercentage ( this . _value ) ;
141
+
142
+ // Since this also modifies the percentage, we need to let the change detection know.
143
+ this . _changeDetectorRef . markForCheck ( ) ;
138
144
}
139
145
private _max : number = 100 ;
140
146
141
147
/** The minimum value that the slider can have. */
142
148
@Input ( )
143
- get min ( ) {
144
- return this . _min ;
145
- }
149
+ get min ( ) { return this . _min ; }
146
150
set min ( v : number ) {
147
151
this . _min = coerceNumberProperty ( v , this . _min ) ;
148
152
@@ -151,6 +155,9 @@ export class MdSlider extends _MdSliderMixinBase
151
155
this . value = this . _min ;
152
156
}
153
157
this . _percent = this . _calculatePercentage ( this . _value ) ;
158
+
159
+ // Since this also modifies the percentage, we need to let the change detection know.
160
+ this . _changeDetectorRef . markForCheck ( ) ;
154
161
}
155
162
private _min : number = 0 ;
156
163
@@ -163,6 +170,9 @@ export class MdSlider extends _MdSliderMixinBase
163
170
if ( this . _step % 1 !== 0 ) {
164
171
this . _roundLabelTo = this . _step . toString ( ) . split ( '.' ) . pop ( ) ! . length ;
165
172
}
173
+
174
+ // Since this could modify the label, we need to notify the change detection.
175
+ this . _changeDetectorRef . markForCheck ( ) ;
166
176
}
167
177
private _step : number = 1 ;
168
178
@@ -209,15 +219,22 @@ export class MdSlider extends _MdSliderMixinBase
209
219
return this . _value ;
210
220
}
211
221
set value ( v : number | null ) {
212
- this . _value = coerceNumberProperty ( v , this . _value || 0 ) ;
213
- this . _percent = this . _calculatePercentage ( this . _value ) ;
222
+ if ( v !== this . _value ) {
223
+ this . _value = coerceNumberProperty ( v , this . _value || 0 ) ;
224
+ this . _percent = this . _calculatePercentage ( this . _value ) ;
225
+
226
+ // Since this also modifies the percentage, we need to let the change detection know.
227
+ this . _changeDetectorRef . markForCheck ( ) ;
228
+ }
214
229
}
215
230
private _value : number | null = null ;
216
231
217
232
/** Whether the slider is vertical. */
218
233
@Input ( )
219
234
get vertical ( ) { return this . _vertical ; }
220
- set vertical ( value : any ) { this . _vertical = coerceBooleanProperty ( value ) ; }
235
+ set vertical ( value : any ) {
236
+ this . _vertical = coerceBooleanProperty ( value ) ;
237
+ }
221
238
private _vertical = false ;
222
239
223
240
@Input ( ) color : 'primary' | 'accent' | 'warn' = 'accent' ;
@@ -392,9 +409,11 @@ export class MdSlider extends _MdSliderMixinBase
392
409
393
410
constructor ( renderer : Renderer2 , private _elementRef : ElementRef ,
394
411
private _focusOriginMonitor : FocusOriginMonitor ,
412
+ private _changeDetectorRef : ChangeDetectorRef ,
395
413
@Optional ( ) private _dir : Directionality ) {
396
414
super ( ) ;
397
- this . _focusOriginMonitor . monitor ( this . _elementRef . nativeElement , renderer , true )
415
+ this . _focusOriginMonitor
416
+ . monitor ( this . _elementRef . nativeElement , renderer , true )
398
417
. subscribe ( ( origin : FocusOrigin ) => this . _isActive = ! ! origin && origin !== 'keyboard' ) ;
399
418
this . _renderer = new SliderRenderer ( this . _elementRef ) ;
400
419
}
0 commit comments