Angular 2 rollback model state in forms

天大地大妈咪最大 提交于 2019-12-20 04:22:05

问题


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).



回答2:


If you make a copy of your model by doing Object.assign({}, yourModel) you won't mutate the original object, which will allow you to revert to the original object.



来源:https://stackoverflow.com/questions/37447703/angular-2-rollback-model-state-in-forms

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!