您好,登錄后才能下訂單哦!
表單的創建
1、Reactive forms
響應式表單,使用顯式和不可變的方法來管理表單在給定時間點的狀態,對表單狀態的每個更改都返回一個新狀態,該狀態在更改之間維護模型的完整性。
幾個關鍵詞:FormControl/[formControl]; FormGroup/[formGroup]; [formControlName]; FormBuilder;
響應式的表單最主要的是form control
name = new FormControl('');
//常用的參數有formState表示值,validators:[]表示驗證new FormControl('abc',[Validators.required])
<label>
Name:
<input type="text" [formControl]="name">
</label>
<p>
Value: {{ name.value }}
</p>
updateName() {
this.name.setValue('Nancy');
}
getName(){
return this.name.value; //value 是 readonly
}
當多個form control組合在一起的時候使用FormGroup
profileForm = new FormGroup({
firstName: new FormControl('',[Validators.required]),
lastName: new FormControl(''),
address:new FormGroup({
street: new FormControl('')
})
});
//另外一種方式
constructor(private fb: FormBuilder) { }
profileForm = this.fb.group({
firstName: ['', Validators.required],
lastName: [''],
});
<form [formGroup]="profileForm">
<label>
First Name:
<input type="text" formControlName="firstName">
</label>
<label>
Last Name:
<input type="text" formControlName="lastName">
</label>
<div formGroupName="address">
<h4>Address</h4>
<label>
Street:
<input type="text" formControlName="street">
</label>
</div>
</form>
//獲取form control
this.profileForm.get('firstName');
//批量更新
this.profileForm.setValue(valueObj);//must all key exist
this.profileForm.patchValue({
firstName: 'Nancy',
address: {
street: '123 Drew Street'
}
});
2、Template-driven forms
傳統方式創建form,幾個關鍵詞:ngModel; ngForm
這種方式創建表單主要是通過model來實現雙向綁定,通過ngForm來將普通的form轉成angular form
<form #heroForm = "ngForm">
<div class="form-group">
<label>Name</label>
<input type="text" class="form-control" name="name" [(ngModel)]="model.name" #name="ngModel">
</div>
</form>
在這個例子中,heroForm有一個屬性form,heroForm.form其實就和FormGroup創建的form一樣了,
設置了#name="ngModel",就可以用heroForm.controls.name來獲取name了
表單的驗證
整個表單的驗證
Reactive form通過profileForm.invalid
Template-driven form通過heroForm.form.invalid
單個字段的驗證
Reactive form直接獲取即可
<form [formGroup] = "profileForm">
<input type="text" formControlName="lastName">
<div *ngIf="lastName.invalid && (lastName.dirty || lastName.touched)"
class="alert alert-danger">
<div *ngIf="lastName.errors.maxlength">
Name must be at most 10 characters long.
</div>
</div>
</form>
如果formControlName是一個變量,用下面這種方式
<form [formGroup] = "profileForm">
<input type="text" [formControlName]="xxx">
<div *ngIf="profileForm.get('xxx').invalid && (profileForm.get('xxx').dirty || profileForm.get('xxx').touched)"
class="alert alert-danger">
<div *ngIf="profileForm.get('xxx').errors.maxlength">
Name must be at most 10 characters long.
</div>
</div>
</form>
Template-driven通過變量判斷
<input type="text" class="form-control" name="name" [(ngModel)]="model.name" required minlength="4" appForbiddenName="11111" #name="ngModel">
<p *ngIf="name.invalid" class="alert alert-danger">
<span *ngIf="name.errors.required">Name is required</span>
<span *ngIf="name.errors.minlength">Min 4 chars</span>
<span *ngIf="name.errors.forbiddenName">This is forbidden name</span>
</p>
一定要有#name="ngModel"這句,這句是定義一個名稱為name的form control
表單的提交
1、通過form的(ngSubmit)="onSubmit()",怎么觸發form的提交,和傳統form是一樣的
2、通過按鈕的click事件
Reactive form
<button click="onSubmit()" class="btn btn-success" [disabled]="profileForm.invalid">Submit</button>
Template-driven form
<button click="onSubmit()" class="btn btn-success" [disabled]="heroForm.form.invalid">Submit</button>
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。