[英]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.