简体   繁体   English

单击取消按钮或编辑图标时,如何删除(删除)现有angularjs表单中未保存的更改

[英]How to remove (delete) the unsaved changes in an existing angularjs form when clicked on cancel button or edit icon

1. the scenario is that there are multiple forms in the page and 1.场景是页面中有多种形式,并且

2. when a user fills in the input fields in one of the form and doesn't save them, those changes should be deleted as soon as they close the current form by the cancel button on the close icon. 2.当用户在一种表单中填写输入字段并且不保存它们时,这些更改应在关闭后立即通过关闭图标上的“取消”按钮删除。

3. if there is any previous data in the input fields that data should be unaltered and if there is any additional data written to it and not saved should be deleted 3.如果输入字段中以前有任何数据,则该数据应保持不变,并且如果有任何其他数据写入该数据且未保存,则应将其删除

HTML: HTML:

 <md-content class="md-padding no-margin">
    <div>
        <form name="formone" novalidate ng-submit="saveData(profile)">
            <div id="basicDetails">
                <div layout="row" layout-align="space- center">
                    <p class="uc-heading">Basic Details</p>
                    <i ng-if="!condition" class="fa-times fa" aria-hidden="true" ng-click="toggleEditPanel('details')"></i>
                </div>
                <div >
                    <label>First name</label>
                    <input ng-model="profile.details" name="details" ng-required="true" />
                    <label>Last name</label>
                    <input ng-model="profile.details.data.data" name="details[[data]]" ng-required="true" />
                </div>
                <div flex layout="row" layout-align="end center">
                    <md-button ng-if="!condition" class="md-raised md-custom-button" ng-click="toggleEditPanel('details')">Cancel</md-button>
                    <md-button type="submit" ng-disabled="profileForm.$invalid" class="md-raised md-custom-button" ></md-button>
                </div>
            </div>
        </form>
    </div>
</md-content>

You need to add two things to your controller: 您需要向控制器添加两件事:

  1. On init, save the current value of profile.details and profile.details.data.data . 初始化时,保存profile.detailsprofile.details.data.data的当前值。
  2. Adde a method resotreData() to your controller where you reassing the saved values to profile.details and profile.details.data.data . 在您的控制器中添加方法resotreData() ,在此您将保存的值重新设置为profile.detailsprofile.details.data.data

And then add resotreData() to the ng-click event of your cancel button. 然后将resotreData()添加到取消按钮的ng-click事件中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM