Number input fires valueChanges twice #12540
Comments
|
Hmm, yeah, looks like a bug to me. We're listening to both events https://github.com/angular/angular/blob/master/modules/%40angular/forms/src/directives/number_value_accessor.ts#L34-L35 - depending on the browser, that'll fire twice in certain scenarios. cc @kara |
|
Curious if this is going to be fixed soon. |
|
This has been open for quite a while, is it going to be fixed soon? |
|
@buddyackerman browser? |
|
@DzmitryShylovich Chrome, FF, IE11 |
|
We will be able to fix it when #12990 will be merged |
|
@DzmitryShylovich OK, any idea when that will be? |
|
no idea :) |
|
Wondering when this will be fixed. I think it is causing the same issue that I am facing.
Using a radio button for this. When the value changes to 'Yes', it is working as expected. But when 'No' is selected, it does not disable my control group. Not positive if these are related. Please help. Thx |
|
+1 |
|
+1 "@angular/forms": "^4.0.0" |
|
am still getting this issue in latest angular, any idea on a fix date? |
|
same issue in "@angular/forms": "4.2.3", |
|
This is actually a huge issue for us. We send significant data back to the server on some changes (customer requirement for autosave) and it is causing our application to be just too slow to use angular reactive forms. Is there any chance at all that this will be fixed soon? |
|
+1 |
|
Same problem as you @michellequack. As a temporary workaround you may want to use distinctUntilChanged |
|
Thanks @tomas-cani . It works perfectly. Used Observable.fromEvent() to capture the (onchange) event. I am using it for file upload feature. |
|
same issue in "@angular/forms": "5.1.3" |
This comment has been minimized.
This comment has been minimized.
|
Having this same issue on v5.2.9. Causing multiple API hits on component load. |
|
Same issue on v5.2.10, any idea ? |
|
Temporary solution: RXJS debounceTime operator: https://www.learnrxjs.io/operators/filtering/debouncetime.html` |
|
https://gist.github.com/SLGShark6/806b5f756db48dbe69da24fbaa92e23d Edit: Tested Angular 8+. |
|
Just an FYI that this basically makes number fields impossible to use with async validators |
|
I experienced a similiar issue with text input fields, but it was a different problem. |
|
FWIW I was able to fix the number fields issue I mentioned above by creating a custom directive extending import {Directive, HostListener, forwardRef} from '@angular/core';
import {NumberValueAccessor, NG_VALUE_ACCESSOR} from '@angular/forms';
export const FIXED_NUMBER_VALUE_ACCESSOR: any = {
provide: NG_VALUE_ACCESSOR,
useExisting: forwardRef(() => FixedNumberValueAccessor),
multi: true
};
@Directive({
selector: 'input[type=number][formControl][fixedNumberField],input[type=number][formControlName][fixedNumberField],input[type=number][ngModel][fixedNumberField]',
providers: [FIXED_NUMBER_VALUE_ACCESSOR],
})
export class FixedNumberValueAccessor extends NumberValueAccessor {
private lastValue: string;
@HostListener('change', ['$event.target.value']) handleOnChange(value) {
this.handleUpdate(value);
}
@HostListener('input', ['$event.target.value']) handleOnInput(value) {
this.handleUpdate(value);
}
private handleUpdate(value: string) {
if (value === this.lastValue) {
return;
}
this.lastValue = value;
this.onChange(value);
}
}Would love to see a longer-term fixed prioritized and merged into core |
See my comment above, for just completely replacing it in the DI container, |
|
+1 |
|
+1 |
|
+1 |
|
+1 |
This comment has been minimized.
This comment has been minimized.
|
+1 |
Prior to this commit, input field of type number used to fire valueChanges twice. First time after typing in the input field and second time when the input field loses the focus. PR Close angular#12540
Prior to this commit, input field of type number used to fire valueChanges twice. First time after typing in the input field and second time when the input field loses the focus. The `input event` is enough for valueChanges Observable, and is supported by both modern and old browsers like IE9-11. PR Close angular#12540
Prior to this commit, input field of type number used to fire valueChanges twice. First time after typing in the input field and second time when the input field loses the focus. The `input event` is enough for valueChanges Observable, and is supported by both modern and old browsers like IE9-11. PR Close angular#12540
Prior to this commit, input field of type number used to fire valueChanges twice. First time after typing in the input field and second time when the input field loses the focus. The `input event` is enough for valueChanges Observable. BREAKING CHANGE: <description> (tests that could break because they rely on 'change' event, requirement for devs to add their own `(change)` listener if they want to support IE9 in the same way it is now, etc) PR Close angular#12540
Prior to this commit, input field of type number used to fire valueChanges twice. First time after typing in the input field and second time when the input field loses the focus. The `input event` is enough for valueChanges Observable. BREAKING CHANGE: <description> (tests that could break because they rely on 'change' event, requirement for devs to add their own `(change)` listener if they want to support IE9 in the same way it is now, etc) PR Close angular#12540
Prior to this commit, input field of type number used to fire valueChanges twice. First time after typing in the input field and second time when the input field loses the focus. The `input event` is enough for valueChanges Observable. BREAKING CHANGE: <description> (tests that could break because they rely on 'change' event, requirement for devs to add their own `(change)` listener if they want to support IE9 in the same way it is now, etc) PR Close angular#12540
Prior to this commit, input field of type number used to fire valueChanges twice. First time after typing in the input field and second time when the input field loses the focus. The `input event` is enough for valueChanges Observable. BREAKING CHANGE: We no longer listen to the `change` event. So, All tests that rely on `change` event could break. We should now listen to `input` event. Additionally, this change breaks IE9, because input events do not fire with backspace or cut actions on IE9. Devs who want to support IE9, will have to add their own `change` listener. PR Close angular#12540
Prior to this commit, number input fields would to fire valueChanges twice: once for `input` events when typing and second for the `change` event when the field lost focus (both events happen at once when using the increment and decrement buttons on the number field). Fixes angular#12540 BREAKING CHANGE: Number inputs no longer listen to the `change` event. * Tests which trigger `change` events need to be updated to trigger `input` events instead. * The `change` event was in place to support IE9, as we found that `input` events were not fired with backspace or cut actions. If you need to maintain IE9 support, you will need to add a change event listener to number inputs and call the `onChange` method of `NumberValueAccessor` manually. * Lastly, old versions of WebDriver would synthetically trigger the `change` event on `WebElement.clear` and `WebElement.sendKeys`. If you are using an old version of WebDriver, you may need to update tests to ensure `input` events are triggered. For example, you could use `element.sendKeys(Keys.chord(Keys.CONTROL, "a"), Keys.BACK_SPACE);` in place of `element.clear()`. PR Close angular#12540
Prior to this commit, number input fields would to fire valueChanges twice: once for `input` events when typing and second for the `change` event when the field lost focus (both events happen at once when using the increment and decrement buttons on the number field). Fixes angular#12540 BREAKING CHANGE: Number inputs no longer listen to the `change` event. * Tests which trigger `change` events need to be updated to trigger `input` events instead. * The `change` event was in place to support IE9, as we found that `input` events were not fired with backspace or cut actions. If you need to maintain IE9 support, you will need to add a change event listener to number inputs and call the `onChange` method of `NumberValueAccessor` manually. * Lastly, old versions of WebDriver would synthetically trigger the `change` event on `WebElement.clear` and `WebElement.sendKeys`. If you are using an old version of WebDriver, you may need to update tests to ensure `input` events are triggered. For example, you could use `element.sendKeys(Keys.chord(Keys.CONTROL, "a"), Keys.BACK_SPACE);` in place of `element.clear()`. PR Close angular#12540
|
Right on! |
|
OMG I can't believe it! I'll have to go back and remove all the workarounds now We needed a pandemic to have the time to fix this issue :D |
|
I was about to file an issue: https://stackblitz.com/edit/angular-ahq3v1?file=src/app/hello.component.ts Wow, its been open for almost 4 years despite being a performance killer and easy to fix. Thanks Alex for fixing <3 |
That wasn't quite it. There was a blocker to fixing this that was finally resolved. |
|
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. |
Prior to this commit, number input fields would to fire valueChanges twice: once for `input` events when typing and second for the `change` event when the field lost focus (both events happen at once when using the increment and decrement buttons on the number field). Fixes angular#12540 BREAKING CHANGE: Number inputs no longer listen to the `change` event. * Tests which trigger `change` events need to be updated to trigger `input` events instead. * The `change` event was in place to support IE9, as we found that `input` events were not fired with backspace or cut actions. If you need to maintain IE9 support, you will need to add a change event listener to number inputs and call the `onChange` method of `NumberValueAccessor` manually. * Lastly, old versions of WebDriver would synthetically trigger the `change` event on `WebElement.clear` and `WebElement.sendKeys`. If you are using an old version of WebDriver, you may need to update tests to ensure `input` events are triggered. For example, you could use `element.sendKeys(Keys.chord(Keys.CONTROL, "a"), Keys.BACK_SPACE);` in place of `element.clear()`. PR Close angular#12540 PR Close angular#36087
May be bug.
ReactiveFormsModule. Input with type "number" fires valueChanges event twice when changed from browser.
I expect it to fire valueChanges once.
Here is a plunkr example: https://plnkr.co/edit/HYCtlP72W3edsaZe9HtR?p=preview
I use ngrx/store to manage state and subscribe my store to valueCahnges observable. When I change value of input, reducers fires twice. It is not optimal, I suppose.
Angular version: 2.1.0
Browser: Chrome
The text was updated successfully, but these errors were encountered: