表单的创建
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进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。