使用NgModel angular 7

时间:2019-06-23 20:26:23

标签: angular forms validation ngmodel

如果您找到解决方案,我不知道我的Ngclass怎么工作

在我的HTML中:

<form [formGroup]="pageForm">
 ... 
 <input [(ngModel)]="namePage" type="text" class="form-control">
 ... 

<div class="d-flex mt-4">
  <i [ngClass]="{ 'is-valid': !f.name.errors }"></i>
</div>

</form>

在我的TS中:

pageForm: FormGroup;
namePage: string;

// the form
this.pageForm = new FormGroup({
  name: new FormControl([this.namePage, Validators.required]),
 });

get f() {
 return this.pageForm.controls;
}

1 个答案:

答案 0 :(得分:0)

尝试


isValid: boolean = false;

get f() {
 this.isValid = this.pageForm.valid;
}
<form [formGroup]="pageForm"  (ngSubmit)="f()">
 ... 
 <input [(ngModel)]="namePage" type="text" class="form-control">
 ... 

<div class="d-flex mt-4">
  <i [ngClass]="{ 'is-valid': isValid }"></i>
</div>

</form>