[英]How to validate form element without using reactive forms?
I want to validate an element without using Reactive Forms, I am using two way binding like below:我想在不使用反应式表单的情况下验证元素,我使用如下两种方式绑定:
<input
type="text"
class="form-control"
autocomplete="off"
[required]="fieldDefinition.required"
[disabled]="fieldDefinition.disabled"
[(ngModel)]="fieldDefinition.fieldvalue"
/>
How to highlight elements by red color after clicking on a button if they are required?如果需要,单击按钮后如何以红色突出显示元素? So, I need to validate all fields after button click.
因此,我需要在单击按钮后验证所有字段。
Angular add the class ng-invalid ng-touched to the inputs with [(ngModel)] -also if you use ReactiveForms Angular 使用 [(ngModel)] 将类 ng-invalid ng-touched 添加到输入中 - 如果您使用 ReactiveForms
so, you can declare a variable所以,你可以声明一个变量
submitted=false;
And in button并在按钮
<button (click)="submitted=true">submit</button>
Your inputs add the class submitted when the variable becomes true您的输入添加了变量变为真时提交的类
<input [class.submitted]="submitted" ....>
And you can define a .css like你可以定义一个 .css 像
.submitted.ng-invalid
{
border-color:red
}
You can see a simple example in this stackblitz你可以在这个 stackblitz 中看到一个简单的例子
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.