Angular2.x Form

表單的建立
一、Reactive forms
響應式表單,使用顯式和不可變的方法來管理表單在給定時間點的狀態,對錶單狀態的每一個更改都返回一個新狀態,該狀態在更改之間維護模型的完整性。
幾個關鍵詞:FormControl/[formControl]; FormGroup/[formGroup]; [formControlName]; FormBuilder; app

響應式的表單最主要的是form controlide

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組合在一塊兒的時候使用FormGroupui

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">
        <h3>Address</h3>

        <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'
    }
});

二、Template-driven forms
傳統方式建立form,幾個關鍵詞:ngModel; ngForm
這種方式建立表單主要是經過model來實現雙向綁定,經過ngForm來將普通的form轉成angular formthis

<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了spa

表單的驗證
整個表單的驗證
Reactive form經過profileForm.invalid
Template-driven form經過heroForm.form.invalid雙向綁定

單個字段的驗證
Reactive form直接獲取便可code

<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是一個變量,用下面這種方式orm

<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 controlget

表單的提交
一、經過form的(ngSubmit)="onSubmit()",怎麼觸發form的提交,和傳統form是同樣的
二、經過按鈕的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>
相關文章
相關標籤/搜索