-
Notifications
You must be signed in to change notification settings - Fork 24.8k
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
Add a way to set new default values to reactive form controls #54022
Comments
Thank you for looking into this issue! |
Hey @DerChris173, import { FormControl } from '@angular/forms';
class ExampleComponent {
myFormControl: FormControl;
constructor() {
this.myFormControl = new FormControl('initialValue'); // Initial default value
}
updateDefaultValue() {
const newDefaultValue = 'updatedValue';
this.myFormControl.setDefaultValue(newDefaultValue); // Proposed method to update default value
}
resetToDefaultValue() {
this.myFormControl.reset(); // Resets to 'updatedValue', if updateDefaultValue() was called
}
} Let's say a user updates their profile name from 'John Doe' (default) to 'Jane Smith'. Currently, reopening the form for more edits would show 'John Doe' again. The suggested setDefaultValue method would allow the form to show 'Jane Smith' as the default, aligning with the latest data and improving UX. |
Hi @alxhub! Thank you for looking at this ticket! I saw that you adjusted the milestone. Does it mean that such a feature would be accepted? I could work on a PR. I already signed the CLA. |
@DerChris173, this feature request would either require a community vote or the Angular teams internal agreement on this. 😉 I could assist you with developing this feature, if continued. |
This feature request is now candidate for our backlog! In the next phase, the community has 60 days to upvote. If the request receives more than 20 upvotes, we'll move it to our consideration list. You can find more details about the feature request process in our documentation. |
I would prefer an additional option on |
Permite emissão de evento com `(p-change-model)` após resetar formulário reativo Modificação feita por conta de um bug que impedia a emissão do evento após resetar o formulário reativo no Angular. angular/angular#54022 angular/angular#52135 angular/angular#50971 angular/angular#46458 angular/angular#15741 Fixes DTHFUI-7232
Permite emissão de evento com `(p-change-model)` após resetar formulário reativo Modificação feita por conta de um bug que impedia a emissão do evento após resetar o formulário reativo no Angular. angular/angular#54022 angular/angular#52135 angular/angular#50971 angular/angular#46458 angular/angular#15741 Fixes DTHFUI-7232
Permite emissão de evento com `(p-change-model)` após resetar formulário reativo Modificação feita por conta de um bug que impedia a emissão do evento após resetar o formulário reativo no Angular. angular/angular#54022 angular/angular#52135 angular/angular#50971 angular/angular#46458 angular/angular#15741 Fixes DTHFUI-7232
Without expanding too much the API surface, we could remove the Would there be any unwanted side effects if we do this ? |
Which @angular/* package(s) are relevant/related to the feature request?
forms
Description
For typical CRUD operations, it's often required to
The defaultValue/reset() Feature is quite suitable for this. Unfortunately, there's no way to change the defaultValue, e.g. once the user performs a "Save action".
Proposed solution
I propose to enhance the Reactive FormControl API with the following methods:
setDefaultValue(newDefaultValue: TValue)
: Updates the defaultValue of the FormControl tonewDefaultValue
. Calls to the reset() operation in particular will use this new defaultValue.setCurrentValuesAsDefault()
: Convenience method forformControl.setDefaultValue(formControl.getRawValue())
.I'd be happy to develop and raise a PR for this feature.
Alternatives considered
Another alternative would be to build entirely new FormControls if new default values are required. However, this might be less convenient and has issues currently with 2-way-data-binding, I believe.
The text was updated successfully, but these errors were encountered: