简体   繁体   English

如何在angular9中手动将Angular模板驱动形式设置为无效

[英]How to manually set an Angular template driven form as invalid in angular9

I want to manually set the EmployeeForm as Invalid from the.ts file.我想从 .ts 文件中手动将EmployeeForm设置为无效。 Here EmployeeForm is a Template driven form.这里EmployeeForm是一个模板驱动的表单。

I have tried to do the following but it didn't work.我曾尝试执行以下操作,但没有成功。

this.EmployeeForm.setErrors({ 'invalid': true }); 

error message says:错误信息说:

Property 'setErrors' does not exist on type 'NgForm'. “NgForm”类型上不存在属性“setErrors”。 Did you mean 'getError' ?您的意思是 'getError'吗?

The same code works when I use it with other forms which are Reactive forms当我将它与其他 forms 一起使用时,相同的代码有效,这些forms

Finally this worked最后这工作

this.EmployeeForm.form.setErrors({ 'invalid': true });

You can add the template reference to your control input.您可以将模板引用添加到您的控制输入。 Then, you can access the form control and add the errors which you want.然后,您可以访问表单控件并添加所需的错误。

Ex: <input type="text" class="form-control" id="name" required [(ngModel)]="model.name" name="name" #name="ngModel">例如: <input type="text" class="form-control" id="name" required [(ngModel)]="model.name" name="name" #name="ngModel">

Reference: Angular Docs .参考: Angular 文档

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

相关问题 Angular:提交模板驱动表单时,如何将焦点设置在第一个无效输入上 - Angular : When submitting Template Driven form how can I set focus on first invalid input 如何设置角度2中的模板驱动表单输入的默认值? - How to set default value of a template driven form input in angular 2? 如何使用角度模板驱动形式的垫自动选择设置过滤器 - How to set filter with mat auto select in angular template driven form 未以反应形式设置输出值 - angular9 - not set ouput value in reactive form - angular9 如何手动将 Angular 表单字段设置为无效? - How can I manually set an Angular form field as invalid? 手动设置表格到无效的角度2+ - set form manually to invalid angular 2+ 如何收听角度模板驱动的表单更改 - how to listen of angular template driven form changes 如何使用双向绑定以及模板驱动的表单验证设置 Angular2 模板驱动表单元素 - How set up Angular2 Template Driven form element with two-way binding as well as template driven form validation 想要在 angular 中设置模板驱动表单的表单组件的值吗? - Want to set the values to form component of template driven form in angular? 在 Angular 7 中初始化模板驱动表单 - Initializing Template Driven Form in Angular 7
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM