How to bind custom error to a Template Driven form input field in angular5

断了今生、忘了曾经 提交于 2019-12-20 01:03:48

问题


In case of Model Driven form or Reactive form we can bind the custom error to an input field as follows:

In Component:

sampleForm.controls["formControlName"].setErrors({ 'incorrect': true });

My question is how can we do the same thing in case of Template Driven form?


回答1:


You can make use of View Child in this scenario get an instance of the ngForm in the component backend model and then add all the validations and errors to it

Something like this

f: NgForm; // f is nothing but the template reference of the Template Driven Form
@ViewChild('f') currentForm: NgForm;
currentForm.form.controls["formControlName"].setErrors({ 'incorrect': true });

For More detailed info check my ts file which i created for such a scenario link



来源:https://stackoverflow.com/questions/48297086/how-to-bind-custom-error-to-a-template-driven-form-input-field-in-angular5

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!