I have a form in Angular 2 which is bound to a model. Now I want the changes in the model to rollback when the user hits cancel. How do I achieve this?
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
回答1:
I'll repeat what was discussed in the comments (because I don't like unanswered questions on SO):
- For many use cases, you can probably use something very similar to the RestoreService that is discussed in the Hierarchical Injectors dev guide.
- For the OP's particular case, where the model is bound to another component and live form changes need to be reflected in the other component, make a copy of your model before displaying the form. If the user cancels, revert to the copy, and notify the other component to use the copy also (i.e., send it the copy).