Reset changes on angular reactive form [closed]
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}
I developped a reactive form in angular :
Select dropdown List ===> [id] = 1, 2, 3....etc
input text ===> name : .....
input text ===> adress : .....
input text ===> Description : .....
I fill the the form by calling a http get service based on selected id in both ngOnInit and (change) handler on select dropdown List
I have two buttons : "validate" and "cancel" Modifications
User can modify inputs data values and save modifications on validate button click.
What is the the best way to cancel modifications done by the user?
javascript html angular typescript
closed as primarily opinion-based by Rob, david, Billal Begueradj, Rafael, rickvdbosch Jan 4 at 6:36
Many good questions generate some degree of opinion based on expert experience, but answers to this question will tend to be almost entirely based on opinions, rather than facts, references, or specific expertise. If this question can be reworded to fit the rules in the help center, please edit the question.
|
show 2 more comments
I developped a reactive form in angular :
Select dropdown List ===> [id] = 1, 2, 3....etc
input text ===> name : .....
input text ===> adress : .....
input text ===> Description : .....
I fill the the form by calling a http get service based on selected id in both ngOnInit and (change) handler on select dropdown List
I have two buttons : "validate" and "cancel" Modifications
User can modify inputs data values and save modifications on validate button click.
What is the the best way to cancel modifications done by the user?
javascript html angular typescript
closed as primarily opinion-based by Rob, david, Billal Begueradj, Rafael, rickvdbosch Jan 4 at 6:36
Many good questions generate some degree of opinion based on expert experience, but answers to this question will tend to be almost entirely based on opinions, rather than facts, references, or specific expertise. If this question can be reworded to fit the rules in the help center, please edit the question.
sending the same request again would be easy :p
– George Bailey
Jan 3 at 21:12
Welcome to stack overflow! In addition to the above option, if you are using reactive forms you should still have a copy of the original object. Just reset the values from the object you have.
– DeborahK
Jan 3 at 21:14
"best" how? What is and what's the problem with your current implementation?
– jonrsharpe
Jan 3 at 21:14
I would use a function to load all values on the initial start of the page, and add a button that calls that function again to 'reset' the form
– Oli
Jan 3 at 21:17
I didtn wanted to recall the service after each cancel, it is not practical, especially in the real case I have a big forum and that takes a little time to be filled, so from what I understood , when calling the service I will save the data in an object !
– Abadou
Jan 3 at 21:53
|
show 2 more comments
I developped a reactive form in angular :
Select dropdown List ===> [id] = 1, 2, 3....etc
input text ===> name : .....
input text ===> adress : .....
input text ===> Description : .....
I fill the the form by calling a http get service based on selected id in both ngOnInit and (change) handler on select dropdown List
I have two buttons : "validate" and "cancel" Modifications
User can modify inputs data values and save modifications on validate button click.
What is the the best way to cancel modifications done by the user?
javascript html angular typescript
I developped a reactive form in angular :
Select dropdown List ===> [id] = 1, 2, 3....etc
input text ===> name : .....
input text ===> adress : .....
input text ===> Description : .....
I fill the the form by calling a http get service based on selected id in both ngOnInit and (change) handler on select dropdown List
I have two buttons : "validate" and "cancel" Modifications
User can modify inputs data values and save modifications on validate button click.
What is the the best way to cancel modifications done by the user?
javascript html angular typescript
javascript html angular typescript
edited Jan 3 at 21:14
jonrsharpe
78.9k11110221
78.9k11110221
asked Jan 3 at 21:11
AbadouAbadou
104
104
closed as primarily opinion-based by Rob, david, Billal Begueradj, Rafael, rickvdbosch Jan 4 at 6:36
Many good questions generate some degree of opinion based on expert experience, but answers to this question will tend to be almost entirely based on opinions, rather than facts, references, or specific expertise. If this question can be reworded to fit the rules in the help center, please edit the question.
closed as primarily opinion-based by Rob, david, Billal Begueradj, Rafael, rickvdbosch Jan 4 at 6:36
Many good questions generate some degree of opinion based on expert experience, but answers to this question will tend to be almost entirely based on opinions, rather than facts, references, or specific expertise. If this question can be reworded to fit the rules in the help center, please edit the question.
sending the same request again would be easy :p
– George Bailey
Jan 3 at 21:12
Welcome to stack overflow! In addition to the above option, if you are using reactive forms you should still have a copy of the original object. Just reset the values from the object you have.
– DeborahK
Jan 3 at 21:14
"best" how? What is and what's the problem with your current implementation?
– jonrsharpe
Jan 3 at 21:14
I would use a function to load all values on the initial start of the page, and add a button that calls that function again to 'reset' the form
– Oli
Jan 3 at 21:17
I didtn wanted to recall the service after each cancel, it is not practical, especially in the real case I have a big forum and that takes a little time to be filled, so from what I understood , when calling the service I will save the data in an object !
– Abadou
Jan 3 at 21:53
|
show 2 more comments
sending the same request again would be easy :p
– George Bailey
Jan 3 at 21:12
Welcome to stack overflow! In addition to the above option, if you are using reactive forms you should still have a copy of the original object. Just reset the values from the object you have.
– DeborahK
Jan 3 at 21:14
"best" how? What is and what's the problem with your current implementation?
– jonrsharpe
Jan 3 at 21:14
I would use a function to load all values on the initial start of the page, and add a button that calls that function again to 'reset' the form
– Oli
Jan 3 at 21:17
I didtn wanted to recall the service after each cancel, it is not practical, especially in the real case I have a big forum and that takes a little time to be filled, so from what I understood , when calling the service I will save the data in an object !
– Abadou
Jan 3 at 21:53
sending the same request again would be easy :p
– George Bailey
Jan 3 at 21:12
sending the same request again would be easy :p
– George Bailey
Jan 3 at 21:12
Welcome to stack overflow! In addition to the above option, if you are using reactive forms you should still have a copy of the original object. Just reset the values from the object you have.
– DeborahK
Jan 3 at 21:14
Welcome to stack overflow! In addition to the above option, if you are using reactive forms you should still have a copy of the original object. Just reset the values from the object you have.
– DeborahK
Jan 3 at 21:14
"best" how? What is and what's the problem with your current implementation?
– jonrsharpe
Jan 3 at 21:14
"best" how? What is and what's the problem with your current implementation?
– jonrsharpe
Jan 3 at 21:14
I would use a function to load all values on the initial start of the page, and add a button that calls that function again to 'reset' the form
– Oli
Jan 3 at 21:17
I would use a function to load all values on the initial start of the page, and add a button that calls that function again to 'reset' the form
– Oli
Jan 3 at 21:17
I didtn wanted to recall the service after each cancel, it is not practical, especially in the real case I have a big forum and that takes a little time to be filled, so from what I understood , when calling the service I will save the data in an object !
– Abadou
Jan 3 at 21:53
I didtn wanted to recall the service after each cancel, it is not practical, especially in the real case I have a big forum and that takes a little time to be filled, so from what I understood , when calling the service I will save the data in an object !
– Abadou
Jan 3 at 21:53
|
show 2 more comments
2 Answers
2
active
oldest
votes
When using Reactive forms, your object values are not bound to the form so any changes the user makes do not modify the original object.
You can copy the values from your existing object back over the form values using code like this:
// Update the data on the form
this.productForm.patchValue({
productName: this.product.productName,
productCode: this.product.productCode,
starRating: this.product.starRating,
description: this.product.description
});
Update
I assume your current process is this:
1) In the ngOnInit, retrieve your data and store it in a property, such as product
shown in the code above.
2) Then you populate the form using code similar to the above.
When the user makes changes, the changes are not made to the original property (product
in this example). Rather they are made to the form model defined with the FormGroup.
So all you need to do is repeat the code from step 2 to cancel any changes and return the form to its state as before the user made any updates.
Does that make sense?
I want to cancel changes after modifications by user ! I dont understund what do you mean ? can you give me an example please ? I copy the original object when I subscribe on service ?
– Abadou
Jan 3 at 21:58
If you want to do a SMALL example of what you are trying to accomplish on stackblitz.com, we can provide a more specific answer.
– DeborahK
Jan 4 at 20:26
I have understund your solution, It is ok, thank you @DeborahK
– Abadou
Jan 5 at 13:01
add a comment |
There is a reset method on form controls in angular:
reset(value?: any, options?: {
onlySelf?: boolean;
emitEvent?: boolean;
}): void;
Example
class Component {
form: FormGroup;
resetForm() {
this.form.reset();
}
}
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
When using Reactive forms, your object values are not bound to the form so any changes the user makes do not modify the original object.
You can copy the values from your existing object back over the form values using code like this:
// Update the data on the form
this.productForm.patchValue({
productName: this.product.productName,
productCode: this.product.productCode,
starRating: this.product.starRating,
description: this.product.description
});
Update
I assume your current process is this:
1) In the ngOnInit, retrieve your data and store it in a property, such as product
shown in the code above.
2) Then you populate the form using code similar to the above.
When the user makes changes, the changes are not made to the original property (product
in this example). Rather they are made to the form model defined with the FormGroup.
So all you need to do is repeat the code from step 2 to cancel any changes and return the form to its state as before the user made any updates.
Does that make sense?
I want to cancel changes after modifications by user ! I dont understund what do you mean ? can you give me an example please ? I copy the original object when I subscribe on service ?
– Abadou
Jan 3 at 21:58
If you want to do a SMALL example of what you are trying to accomplish on stackblitz.com, we can provide a more specific answer.
– DeborahK
Jan 4 at 20:26
I have understund your solution, It is ok, thank you @DeborahK
– Abadou
Jan 5 at 13:01
add a comment |
When using Reactive forms, your object values are not bound to the form so any changes the user makes do not modify the original object.
You can copy the values from your existing object back over the form values using code like this:
// Update the data on the form
this.productForm.patchValue({
productName: this.product.productName,
productCode: this.product.productCode,
starRating: this.product.starRating,
description: this.product.description
});
Update
I assume your current process is this:
1) In the ngOnInit, retrieve your data and store it in a property, such as product
shown in the code above.
2) Then you populate the form using code similar to the above.
When the user makes changes, the changes are not made to the original property (product
in this example). Rather they are made to the form model defined with the FormGroup.
So all you need to do is repeat the code from step 2 to cancel any changes and return the form to its state as before the user made any updates.
Does that make sense?
I want to cancel changes after modifications by user ! I dont understund what do you mean ? can you give me an example please ? I copy the original object when I subscribe on service ?
– Abadou
Jan 3 at 21:58
If you want to do a SMALL example of what you are trying to accomplish on stackblitz.com, we can provide a more specific answer.
– DeborahK
Jan 4 at 20:26
I have understund your solution, It is ok, thank you @DeborahK
– Abadou
Jan 5 at 13:01
add a comment |
When using Reactive forms, your object values are not bound to the form so any changes the user makes do not modify the original object.
You can copy the values from your existing object back over the form values using code like this:
// Update the data on the form
this.productForm.patchValue({
productName: this.product.productName,
productCode: this.product.productCode,
starRating: this.product.starRating,
description: this.product.description
});
Update
I assume your current process is this:
1) In the ngOnInit, retrieve your data and store it in a property, such as product
shown in the code above.
2) Then you populate the form using code similar to the above.
When the user makes changes, the changes are not made to the original property (product
in this example). Rather they are made to the form model defined with the FormGroup.
So all you need to do is repeat the code from step 2 to cancel any changes and return the form to its state as before the user made any updates.
Does that make sense?
When using Reactive forms, your object values are not bound to the form so any changes the user makes do not modify the original object.
You can copy the values from your existing object back over the form values using code like this:
// Update the data on the form
this.productForm.patchValue({
productName: this.product.productName,
productCode: this.product.productCode,
starRating: this.product.starRating,
description: this.product.description
});
Update
I assume your current process is this:
1) In the ngOnInit, retrieve your data and store it in a property, such as product
shown in the code above.
2) Then you populate the form using code similar to the above.
When the user makes changes, the changes are not made to the original property (product
in this example). Rather they are made to the form model defined with the FormGroup.
So all you need to do is repeat the code from step 2 to cancel any changes and return the form to its state as before the user made any updates.
Does that make sense?
edited Jan 5 at 22:36
answered Jan 3 at 21:16
DeborahKDeborahK
29.4k54473
29.4k54473
I want to cancel changes after modifications by user ! I dont understund what do you mean ? can you give me an example please ? I copy the original object when I subscribe on service ?
– Abadou
Jan 3 at 21:58
If you want to do a SMALL example of what you are trying to accomplish on stackblitz.com, we can provide a more specific answer.
– DeborahK
Jan 4 at 20:26
I have understund your solution, It is ok, thank you @DeborahK
– Abadou
Jan 5 at 13:01
add a comment |
I want to cancel changes after modifications by user ! I dont understund what do you mean ? can you give me an example please ? I copy the original object when I subscribe on service ?
– Abadou
Jan 3 at 21:58
If you want to do a SMALL example of what you are trying to accomplish on stackblitz.com, we can provide a more specific answer.
– DeborahK
Jan 4 at 20:26
I have understund your solution, It is ok, thank you @DeborahK
– Abadou
Jan 5 at 13:01
I want to cancel changes after modifications by user ! I dont understund what do you mean ? can you give me an example please ? I copy the original object when I subscribe on service ?
– Abadou
Jan 3 at 21:58
I want to cancel changes after modifications by user ! I dont understund what do you mean ? can you give me an example please ? I copy the original object when I subscribe on service ?
– Abadou
Jan 3 at 21:58
If you want to do a SMALL example of what you are trying to accomplish on stackblitz.com, we can provide a more specific answer.
– DeborahK
Jan 4 at 20:26
If you want to do a SMALL example of what you are trying to accomplish on stackblitz.com, we can provide a more specific answer.
– DeborahK
Jan 4 at 20:26
I have understund your solution, It is ok, thank you @DeborahK
– Abadou
Jan 5 at 13:01
I have understund your solution, It is ok, thank you @DeborahK
– Abadou
Jan 5 at 13:01
add a comment |
There is a reset method on form controls in angular:
reset(value?: any, options?: {
onlySelf?: boolean;
emitEvent?: boolean;
}): void;
Example
class Component {
form: FormGroup;
resetForm() {
this.form.reset();
}
}
add a comment |
There is a reset method on form controls in angular:
reset(value?: any, options?: {
onlySelf?: boolean;
emitEvent?: boolean;
}): void;
Example
class Component {
form: FormGroup;
resetForm() {
this.form.reset();
}
}
add a comment |
There is a reset method on form controls in angular:
reset(value?: any, options?: {
onlySelf?: boolean;
emitEvent?: boolean;
}): void;
Example
class Component {
form: FormGroup;
resetForm() {
this.form.reset();
}
}
There is a reset method on form controls in angular:
reset(value?: any, options?: {
onlySelf?: boolean;
emitEvent?: boolean;
}): void;
Example
class Component {
form: FormGroup;
resetForm() {
this.form.reset();
}
}
answered Jan 3 at 21:51
m.kochm.koch
1042
1042
add a comment |
add a comment |
sending the same request again would be easy :p
– George Bailey
Jan 3 at 21:12
Welcome to stack overflow! In addition to the above option, if you are using reactive forms you should still have a copy of the original object. Just reset the values from the object you have.
– DeborahK
Jan 3 at 21:14
"best" how? What is and what's the problem with your current implementation?
– jonrsharpe
Jan 3 at 21:14
I would use a function to load all values on the initial start of the page, and add a button that calls that function again to 'reset' the form
– Oli
Jan 3 at 21:17
I didtn wanted to recall the service after each cancel, it is not practical, especially in the real case I have a big forum and that takes a little time to be filled, so from what I understood , when calling the service I will save the data in an object !
– Abadou
Jan 3 at 21:53