validation - Angular 2 : How change pristine of NgModel in code?

标签 validation angular typescript

当您更改 NgModel 的字段时,它会自动将 model.prisitne 更改为 true。

当您提交表单时,它不会更改“原始状态”,毫无疑问,这不是错误。

但在我的例子中,当“pristine”为真时,我会显示错误,当我提交表单时,我需要显示验证错误,我想当你提交表单时,我们可以说这个表单中的字段触及了,因为您不能提交无效的表格。但在 Angular2 中它以不同的方式工作。

那么,有什么方法可以说明在代码/组件中触及了表单控件/字段(pristine = true)?

let email:AbstractControl = this.frm.form.controls['email'];

设置电子邮件“prisitne”为真。

最佳答案

email.markAsPristine();
email.markAsTouched();
email.reset();

this.frm.reset();

另见 https://angular.io/docs/ts/latest/api/forms/index/AbstractControl-class.html

您可以使用这种较短的方法来获取控件

let email:AbstractControl = this.frm.get('email']);

关于validation - Angular 2 : How change pristine of NgModel in code?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41014626/

相关文章:

angular - 如何在 Angular Material 表中拆分标题

json - 将 JSON 映射到 TS 类。如果 property 是一个对象并且在 JSON 中找不到,类应该有空 obj

javascript - Angular 4 动态添加点击事件

其余 Controller 参数的 Spring 验证不起作用

ruby-on-rails - Rails 指定验证顺序

angular - Word 中的文本已损坏

angular - catchError 从 web-api 返回错误 500 时不起作用

typescript - Vue.JS 单元测试 - 使用 sinon.stub() 后仍在调用原始方法

java - 使用 Binder 进行表单验证

javascript - Vue/Vuetify 文本字段验证取决于复选框