gpt4 book ai didi

javascript - 将表单设置为 $pristine,但保持当前值

转载 作者:行者123 更新时间:2023-11-30 16:46:52 26 4
gpt4 key购买 nike

我的用例是我需要根据用户是否更改了表单的值来启用/禁用保存按钮。

当用户单击保存按钮(在进行更改后)时,将进行 AJAX 保存,完成后表单需要再次被视为“原始”,但保持当前值

我认为设置 form.$pristine = true 会起作用,它会在单击按钮后第一次执行此操作,但从那里编辑值不会设置 $pristine 为假。

示例 plunker:http://plnkr.co/edit/VX2R1bdGJFdzH3LEsNnB?p=preview

<form name="submitForm">
<input ng-model="data.first" type="text" />
<input ng-model="data.second" type="text" />
</form>
<button ng-click="saveForm()">Submit</button>
<div>Form pristine: {{submitForm.$pristine}}</div>

//In controller
$scope.saveForm = function(){
$scope.submitForm.$pristine = true;
}
  1. 还有其他方法吗?
  2. 如果没有,我需要做什么才能让它发挥作用?

最佳答案

FormController.$setPristine();

Sets the form to its pristine state.

This method can be called to remove the 'ng-dirty' class and set the form to its pristine state (ng-pristine class). This method will also propagate to all the controls contained in this form.

Setting a form back to a pristine state is often useful when we want to 'reuse' a form after saving or resetting it.

关于javascript - 将表单设置为 $pristine,但保持当前值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31094249/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com