angular2 模板驱动型表单
发布时间:2020-12-17 08:39:21 所属栏目:安全 来源:网络整理
导读:angular2 模板驱动型表单 要使用模板驱动型表单,我们必须先导入 FormsModule 模块。 @NgModule({ imports: [ FormsModule,//== CommonModule ],...}) 构造表单获取表单数据 template.component.html form #form="ngForm" (ngSubmit)="onSubmit(form.value)"
angular2 模板驱动型表单要使用模板驱动型表单,我们必须先导入 @NgModule({ imports: [ FormsModule,//<== CommonModule ],... }) 构造表单获取表单数据template.component.html <form #form="ngForm" (ngSubmit)="onSubmit(form.value)"> <div> <label for="">Name: </label> <input type="text" name="name" ngModel> </div> <div> <label for="">Password: </label> <input type="password" name="password" ngModel> </div> <button type="submit">Submit</button> </form> <div> {{form.value | json}} </div> template.component.ts import { Component,OnInit } from '@angular/core'; @Component({ selector: 'app-template',templateUrl: './template.component.html' }) export class TemplateComponent{ constructor() { } onSubmit(value) { console.log('submit',value); } } 几点说明:
添加表单验证显示错误信息假设 template.component.html <form #form="ngForm" (ngSubmit)="onSubmit(form)"> <div> <label for="">Name: </label> <input type="text" name="name" ngModel required #name="ngModel"> <div [style.color]="'red'" *ngIf="name.invalid && (name.dirty || name.touched || form.submitted)"> <p *ngIf="name.errors.required">name can not be empty!</p> </div> </div> <div> <label for="">Password: </label> <input type="password" name="password" ngModel required minlength="6" maxlength="12" #password="ngModel"> <div [style.color]="'red'" *ngIf="password.invalid && (password.dirty || password.touched || form.submitted)"> <p *ngIf="password.errors.required">password can not be empty!</p> <p *ngIf="password.errors.maxlength">the maxlength of password is 12!</p> <p *ngIf="password.errors.minlength">the minlength of password is 6!</p> </div> </div> <button type="submit">Submit</button> </form> 几点说明:
自定义同步验证器假设我们需要一个可以指定验证规则的密码验证器。 import { AbstractControl,NG_VALIDATORS,Validator } from '@angular/forms'; import {Directive,Input} from '@angular/core' @Directive({ selector:'[password]',providers: [{provide: NG_VALIDATORS,useExisting: PasswrodDirective,multi: true}] }) export class PasswrodDirective implements Validator{ @Input() password:string; validate(control:AbstractControl) { let error = null; if (this.password) { const reg = new RegExp(this.password,'i'); if (!reg.test(control.value)) { error = { password:control.value } } } return error; } } 在模板中使用 <input type="password" name="password" ngModel password="^[0-9]{6,12}$" #password="ngModel"> <div class="error" [style.color]="'red'" *ngIf="password.invalid && (password.dirty || password.touched || form.submitted)"> <p *ngIf="password.errors.password">password is illegal!</p> </div> 几点说明:
自定义异步验证器假设我们需要一个可以验证用户名是否重复的验证器。 import { AbstractControl,AsyncValidator,NG_ASYNC_VALIDATORS } from '@angular/forms'; import { Directive } from '@angular/core'; const userList = [ 'jack','mary','jimi','tom' ]; @Directive({ selector:'[checkName]',providers: [{provide: NG_ASYNC_VALIDATORS,useExisting: CheckNameDirective,multi: true}] }) export class CheckNameDirective implements AsyncValidator{ validate(control:AbstractControl) { console.log(control); return new Promise((resolve,reject) => { setTimeout(() => { if (userList.indexOf(control.value) >= 0) { resolve({ 'checkName':control.value }); } else { resolve(null); } },2000) }); } } 在模板中使用 <input type="text" name="name" ngModel required #name="ngModel" checkName> <div class="error" [style.color]="'red'" *ngIf="name.invalid && (name.dirty || name.touched || form.submitted)"> <p *ngIf="name.errors.required">name can not be empty!</p> <p *ngIf="name.errors.checkName">this name is exist!</p> </div> 几点说明:
(编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |