繁体   English   中英

在 Angularjs 中提交后重置表单

[英]Resetting form after submit in Angularjs

嗨,我有一个表单,可以在单击按钮时进行更新。

 $scope.action = "Update";
  var id = $routeParams.editId;
  scope.item = updateRecord.get({ id: id });

项目更新后,它不会删除表单字段中输入的信息。 我想知道 angularjs 中有什么可用在 udpating 之后添加到上面的代码中,以便它也清除形成。 谢谢

您可以通过$scope.formName.$setPristine();重置表单$scope.formName.$setPristine(); 但是如果您将模型对象绑定到您的输入,您也需要注意清除它们,即:

$scope.currentRecord={};

编辑

正如 ToodoN-Mike 指出的那样,不要忘记设置

$scope.formName.$setUntouched()

$touched标志是在 angular 1.3 中引入的。

在提交函数主体的底部运行下面的代码。

// 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”是我的表单名称。

有关更多信息,请查看此文档页面: https : //docs.angularjs.org/api/ng/type/form.FormController

这对我有用。

viewModel.data = {};
$scope.formName.$setUntouched();
$scope.formName.$setPristine();

1) 要删除表单字段中的值并重置您可以使用$setPristine();

$scope.formName.$setPristine();

2) 接下来,也可以使用$setUntouched()将表单设置为 Untouched状态

(如果您的表单字段中有必填字段,并且如果您使用的是ng-messages,那么如果您不使用以下功能,这些字段将显示错误。)

$scope.formName.$setUntouched();

我不明白这个问题,但也许,您可以清理 Html 组件中的表单:

函数:ngSubmit(),发送数据。 taskName 是字段的名称,也是 taskBody。

<form (ngSubmit)="onSubmit(taskName.value, taskBody.value); taskName.value=''; taskBody.value=''" #taskForm="ngForm">

暂无
暂无

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

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