Search code examples
angularjsangular-strap

AngularJs: How can I create a save and discard/cancel pattern in a modal form?


I have a form within a modal, see plnkr here. When editing the menu item, after making some changes, If I click cancel in the modal, I want the default menuitem without any changes being made to it to show up i.e. all changes discarded(). So the next time I go into the modal I meet the default item sans changes.

If I click save I want to keep the changes i.e. run the save method.

Both save and cancle close the modals after being clicked.

How do I create such a save and discard/cancel mechanism?

Are there other angular ways of achieving this effect, different

I am using the modal from angularStrap.


Solution

  • Edited example: http://plnkr.co/edit/OTN4Qh?p=preview

    What was changed:

    Added ng-init='orginalName = menuItem.Name' to the edit menu

    Changed ng-click on the cancel button to ng-click="menuItem.Name = orginalName; dismiss()"

    With these changes, they will see the updates as they type, but when the changes are canceled they will be reverted.

    If wanted, you can reverse this so that the edits are not updated as they type and would only be applied on save.