[英]Nothing happens on touched event in angular 7
<div class="register_input">
<span class="red">*</span>
<label>First Name</label>
<input maxlength="30" formControlName="firstName" type="text" required>
<p>{{contactUsForm.value | json}}</p>
<p *ngIf="firstName.touched">Touched</p>
</div>
Component Snippet: 组件片段:
import {Component} from "@angular/core";
import {Router} from '@angular/router';
import {FormControl, FormGroup, FormBuilder,Validators} from '@angular/forms';
@Component({
selector:'contact us',
templateUrl:'./contact-us.component.html'
})
export class ContactUsComponent {
contactUsForm: FormGroup;
constructor( private route: Router, private formBuilder: FormBuilder) {
}
ngOnInit(){
this.main();
}
main() {
this.contactUsForm = this.formBuilder.group({
firstName: '',
lastName: '',
email: '',
phoneNumber:''
});
}
the <p>
doesn't appear when I click on the input or even if I enter text. 单击输入或输入文字时,
<p>
不会出现。 I am using a reactive form. 我正在使用反应形式。 I have added the component also.
我还添加了该组件。
Replace
firstName.touched
withcontactUsForm.get('firstName').touched
(I thinks contactUsForm is your form name)用
contactUsForm.get('firstName').touched
替换firstName.touched
(我认为contactUsForm是您的表单名称)
<p *ngIf="contactUsForm.get('firstName').touched">Touched</p>
Maybe it will work for you. 也许它将为您工作。
You can also create getter in ts file and use here like 您也可以在ts文件中创建getter并在此处使用
get getFirstName() {
return this.contactUsForm.get('firstName').touched;
}
<p *ngIf="getFirstName.touched">Touched</p>
Try like this: 尝试这样:
create boolean method like isFieldTouched()
with if condition on p
tag 在
p
标签上使用if条件创建类似于isFieldTouched()
布尔方法
<div class="register_input">
<span class="red">*</span>
<label>First Name</label>
<input maxlength="30" formControlName="firstName" type="text" required>
<p>{{contactUsForm.value | json}}</p>
<p *ngIf="isFieldTouched('firstName')">Touched</p>
</div>
ts file ts文件
contactUsForm: FormGroup;
constructor( private formBuilder: FormBuilder) {}
ngOnInit(){
this.main();
}
isFieldTouched(field: string): boolean {
return this.contactUsForm.get(field).touched;
}
main() {
this.contactUsForm = this.formBuilder.group({
firstName: ['',Validators.required],
lastName: ['',Validators.required],
email: ['',Validators.required],
phoneNumber:['',Validators.required],
});
}
This works for me 这对我有用
<form #frm="ngForm">
<div class="register_input">
<span class="red">*</span>
<label>First Name</label>
<input maxlength="30" name="firstName" id="firstName" #firstName="ngModel" [(ngModel)]="firstNameValue" type="text"
required>
<p>{{contactUsForm.value | json}}</p>
<p *ngIf="firstName.touched">Touched</p>
</div>
</form>
Make sure you include the FormModule in your app modules. 确保在您的应用程序模块中包括FormModule。
Edit: If you would like to use reactive forms, it would be like how @Dhawal said, so the full solution would look like this 编辑:如果您想使用反应形式,就像@Dhawal所说的那样,因此完整的解决方案如下所示
html: HTML:
<form [formGroup]="contactUsForm">
<div class="register_input">
<span class="red">*</span>
<label>First Name</label>
<input maxlength="30" formControlName="firstName" type="text" required>
<p>{{contactUsForm.value | json}}</p>
<p *ngIf="contactUsForm.get('firstName').touched">Touched</p>
</div>
</form>
Component: 零件:
export class AppComponent {
contactUsForm: FormGroup;
ngOnInit(){
this.main();
}
main() {
this.contactUsForm = new FormGroup({
firstName: new FormControl()
});
}
to add required validation you can do something like this as Nenad Radak 要添加所需的验证,您可以像Nenad Radak这样执行操作
firstName: ['', Validators.required]
instead of 代替
firstName: new FormControl()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.