[英]Resetting form after submit in Angularjs
Hi I have a form which does update on button click.嗨,我有一个表单,可以在单击按钮时进行更新。
$scope.action = "Update";
var id = $routeParams.editId;
scope.item = updateRecord.get({ id: id });
Once the item is updated it doesn't remove the entered information in the form fields.项目更新后,它不会删除表单字段中输入的信息。 I was wondering what is available in angularjs to add in the above code after udpating so that it also clears to form.我想知道 angularjs 中有什么可用在 udpating 之后添加到上面的代码中,以便它也清除形成。 Thanks谢谢
You can reset a form by, $scope.formName.$setPristine();
您可以通过$scope.formName.$setPristine();
重置表单$scope.formName.$setPristine();
but if you're binding a model object to your inputs, you need to take care of clearing those too, ie:但是如果您将模型对象绑定到您的输入,您也需要注意清除它们,即:
$scope.currentRecord={};
EDIT编辑
As ToodoN-Mike pointed out, don't forget to set正如 ToodoN-Mike 指出的那样,不要忘记设置
$scope.formName.$setUntouched()
The $touched
flag was introduced in angular 1.3. $touched
标志是在 angular 1.3 中引入的。
At the bottom of your submit function's body run this code below.在提交函数主体的底部运行下面的代码。
// Reset the form model.
vm.project = {};
// Set back to pristine.
vm.form.$setPristine();
// Since Angular 1.3, set back to untouched state.
vm.form.$setUntouched();
"vm.form" is my form name. “vm.form”是我的表单名称。
For more info have a look at this docs page: https://docs.angularjs.org/api/ng/type/form.FormController有关更多信息,请查看此文档页面: https : //docs.angularjs.org/api/ng/type/form.FormController
This worked for me.这对我有用。
viewModel.data = {};
$scope.formName.$setUntouched();
$scope.formName.$setPristine();
1) To Remove the values in Form Fields and to reset you can use $setPristine(); 1) 要删除表单字段中的值并重置您可以使用$setPristine();
$scope.formName.$setPristine();
2) Next, to set the form to Untouched State too use $setUntouched(); 2) 接下来,也可以使用$setUntouched()将表单设置为 Untouched状态;
(If you have required fields in your form Fields and also if you are using ng-messages then if you don't use the below function those fields will show error.) (如果您的表单字段中有必填字段,并且如果您使用的是ng-messages,那么如果您不使用以下功能,这些字段将显示错误。)
$scope.formName.$setUntouched();
I dont get the question, but maybe, you can clean the form in the Html component:我不明白这个问题,但也许,您可以清理 Html 组件中的表单:
function: ngSubmit(), send the data.函数:ngSubmit(),发送数据。 taskName is the name of the field, also taskBody. taskName 是字段的名称,也是 taskBody。
<form (ngSubmit)="onSubmit(taskName.value, taskBody.value); taskName.value=''; taskBody.value=''" #taskForm="ngForm">
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.