簡體   English   中英

使用NgModel angular 7進行表單驗證

[英]Form validation with NgModel angular 7

我不知道我的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;
}

嘗試


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>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM