溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

Angular2.x Form

發(fā)布時間:2020-07-28 09:08:36 來源:網(wǎng)絡 閱讀:102 作者:csboyty 欄目:web開發(fā)

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

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

name = new FormControl('');
//常用的參數(shù)有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
傳統(tǒng)方式創(chuàng)建form,幾個關鍵詞:ngModel; ngForm
這種方式創(chuàng)建表單主要是通過model來實現(xiàn)雙向綁定,通過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創(chuàng)建的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()",怎么觸發(fā)form的提交,和傳統(tǒng)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>
向AI問一下細節(jié)

免責聲明:本站發(fā)布的內容(圖片、視頻和文字)以原創(chuàng)、轉載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權內容。

AI