New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
MatSlider: change event fired twice #14363
Comments
It fires immediately if you don't click exactly in the center of the knob. |
It probably because we change the value on |
This is happening to me on every single iOS devices, any suggestions? |
I'm sure an end-user doesn't expect the value to be changed, just because he/she grabbed the knob 1 pixel on the left of the knob center :) I think the behavior should be on the mouseup event, to avoid this situation. |
The problem, if we were to do that, is that the events will no longer be in sync with what is being rendered on screen. |
Confirmed, it's an unexpected behavior (bug). If you click on the thumb if the slider is not focused an begin to drag the change event is emitted. Whatever the component does for internal reasons, the output interface "(change)" should only be fired once on pointer up. You can keep change detection on pointer down internal for the component. But not for the output change. @crisbeto It's only about the output event of the component. You can do anything internal. Inside the component you can use mouse down whatever to render the current value. BUT the interface output (change) event should only be fired once. You could split the output event value with a internal value and only emit the value for output on mouse up / pointer up / etc. |
Workaround: <mat-slider
#mySlider
(slideend)="sliderOnChange(mySlider.value)"
></mat-slider> This is the "real" (expected) "change"-event. UPDATE: UPDATE: <mat-slider
#mySlider
(slideend)="sliderOnChange(mySlider.value)"
(pointerup)="sliderOnChange(mySlider.value)"
></mat-slider> sliderOnChange(value: number) {
if (this.mySliderValue !== value) {
this.mySliderValue = value;
console.log('changed: ', value);
}
} Ok, now it works. THIS is the "real" "change"-event as expected. But just a workaround. |
Well, the |
i have faced same problem please let me know what is right solution of this bug???? |
I have a little work around that seems to work so far... Please don't fire at me if it doesn't for you ;) <mat-slider min="0"
max="100"
step="1"
(input)="sliderSliding( $event )"
(change)="sliderChanged( $event )"
(mouseup)="sliderMouseButtonReleased( $event )"
[(ngModel)]="value">
</mat-slider> public value: number = 0;
private _slidingValue: number;
public sliderSliding( args: MatSliderChange ): void {
this._slidingValue = args.value;
}
public sliderMouseButtonReleased( args: MouseEvent ): void {
if ( this.value === this._slidingValue ) {
this.sliderChanged( null );
}
}
public sliderChanged( args: any ): void {
if ( !!args && !!args.source._lastPointerEvent ) {
return;
}
// Your code to process the event here
} |
For those who - like me - desperately need a workaround: I downloaded the material repository and moved all necessary files to my project. Then, in src/material/slider/slider.ts in method This works for my case, but bear in mind, that I haven't run any automated tests againist this change. Hope this helps |
Thanks! This helped me out a lot, but I noticed that you will still get multiple events fired when using a touch device or if using a keyboard to move the slider. <mat-slider min="0"
max="100"
step="1"
(input)="sliderSliding( $event )"
(mouseup)="sliderChanged( $event )"
(keyup)="sliderChanged( $event )"
(touchend)="sliderChanged( $event )"
[(ngModel)]="value">
</mat-slider> public value: number = 0;
private _slidingValue: number;
public sliderSliding( args: MatSliderChange ): void {
this._slidingValue = args.value;
}
public sliderChanged( args: any ): void {
// Your code to process the event here
} |
touchend event is not emitted by mat-slider. Did you test that pls? |
Appeared to work when I tested it - without touchend and after removing change was not getting any events fired from a touch device. |
pls
touchend doesn't work for me. I used panend (from Hammer.js) event instead of it and it work properly now. Thank you for sharing workaround |
Currently when the user presses down somewhere on the slider we emit a `change` event, as well as when they release. While we don't emit the same value twice, the timing seems to be unexpected by consumers, because one happens at the begging of the sequence and another one at the end. These changes make it so that we only emit the change event once the user has released the slider. Fixes angular#14363.
Bumping to P2 since people seem to continue to run into this. I also submitted a fix at #20240, but I suspect that it might be difficult to get it merged in, because it can break people's tests. |
Currently when the user presses down somewhere on the slider we emit a `change` event, as well as when they release. While we don't emit the same value twice, the timing seems to be unexpected by consumers, because one happens at the begging of the sequence and another one at the end. These changes make it so that we only emit the change event once the user has released the slider. Fixes angular#14363.
Currently when the user presses down somewhere on the slider we emit a `change` event, as well as when they release. While we don't emit the same value twice, the timing seems to be unexpected by consumers, because one happens at the begging of the sequence and another one at the end. These changes make it so that we only emit the change event once the user has released the slider. Fixes #14363. (cherry picked from commit ce72369)
Currently when the user presses down somewhere on the slider we emit a `change` event, as well as when they release. While we don't emit the same value twice, the timing seems to be unexpected by consumers, because one happens at the begging of the sequence and another one at the end. These changes make it so that we only emit the change event once the user has released the slider. Fixes #14363.
Thanks for fixing! Workaround no longer required |
This issue has been automatically locked due to inactivity. Read more about our automatic conversation locking policy. This action has been performed automatically by a bot. |
Currently when the user presses down somewhere on the slider we emit a `change` event, as well as when they release. While we don't emit the same value twice, the timing seems to be unexpected by consumers, because one happens at the begging of the sequence and another one at the end. These changes make it so that we only emit the change event once the user has released the slider. Fixes angular#14363.
Bug:
MatSlider change event is fired twice when slider knob is dragged and released. Surprisingly enough, this happens only sometimes.
What is the expected behavior?
Event emitted when the slider value has changed.
What is the current behavior?
MatSlider change event is fired twice when slider knob is dragged and released.
What are the steps to reproduce?
See: stackblitz
What is the use-case or motivation for changing an existing behavior?
Which versions of Angular, Material, OS, TypeScript, browsers are affected?
Is there anything else we should know?
The text was updated successfully, but these errors were encountered: