第五章 表单

前端开发 作者: 2024-08-25 19:45:02
表单是最重要也是最复杂的表单可能是WEB世界里最重要的,通过表单获取用户的输入.另一方面,表单看起来又是简单的,你放置一个input标签,一个submit按钮,然后点击按钮,提交,有什么难的呢? 实践证明,表单确实是很复杂的,理由如下: 表单输入意味着修改服务器与客户端的数据. 改变通常会需

Controls

// create a new Control with the value "Nate" let nameControl = new Control("Nate"); let name = nameControl.value; // -> Nate // now we can query this control for certain values: nameControl.errors // -> StringMap<string,any> of errors nameControl.dirty // -> false nameControl.valid // -> true // etc.
<!-- part of some bigger form --> <input type="text" ngControl="name" />
let personInfo = new ControlGroup({ firstName: new Control("Nate"),lastName: new Control("Murray"),zip: new Control("90210") })
personInfo.value; // -> { // firstName: "Nate", // lastName: "Murray", // zip: "90210" //} // now we can query this control group for certain values,which have sensible // values depending on the children Control's values: personInfo.errors // -> StringMap<string,any> of errors personInfo.dirty // -> false personInfo.valid // -> true // etc.
import { Component } from '@angular/core'; import { FORM_DIRECTIVES } from '@angular/common'; @Component({ selector: 'demo-form-sku',directives: [FORM_DIRECTIVES],
<div class="ui raised segment"> <h2 class="ui header">Demo Form: Sku</h2> <form #f="ngForm" (ngSubmit)="onSubmit(f.value)" class="ui form"> <div class="field"> Forms in Angular 2 127 <label for="skuInput">SKU</label> <input type="text" id="skuInput" placeholder="SKU" ngControl="sku"> <button type="submit" class="ui button">Submit</button> </div> </form> </div>

form 和 NgForm

<form #f="ngForm" (ngSubmit)="onSubmit(f.value)" class="ui form"> </form>

input和ngControl

<form #f="ngForm" (ngSubmit)="onSubmit(f.value)" class="ui form"> <div class="field"> Forms in Angular 2 127 <label for="skuInput">SKU</label> <input type="text" id="skuInput" placeholder="SKU" ngControl="sku"> <button type="submit" class="ui button">Submit</button> </div> </form>
export class DemoFormSku { onSubmit(form: any): void { console.log('you submitted value:',form); } }
import { Component } from '@angular/core'; import { FORM_DIRECTIVES } from '@angular/common'; @Component({ selector: 'demo-form-sku',template: ` <div class="ui raised segment"> <h2 class="ui header">Demo Form: Sku</h2> <form #f="ngForm" (ngSubmit)="onSubmit(f.value)" class="ui form"> <div class="field"> <label for="skuInput">SKU</label> <input type="text" id="skuInput" placeholder="SKU" ngControl="sku"> </div> <button type="submit" class="ui button">Submit</button> </form> </div> ` }) export class DemoFormSku { onSubmit(form: any): void { console.log('you submitted value:',form); } }

怎样使用FormBuilder

export class DemoFormSkuBuilder { myForm: ControlGroup; constructor(fb: FormBuilder) { this.myForm = fb.group({ 'sku': ['ABC123'] }); } onSubmit(value: string): void { console.log('you submitted value: ',value); }

在view中使用myForm

<form [ngFormModel]="myForm" (ngSubmit)="onSubmit(myForm.value)"
<input type="text" id="skuInput" placeholder="SKU" [ngFormControl]="myForm.controls['sku']">

试试

import { Component } from '@angular/core'; import { FORM_DIRECTIVES,FormBuilder,ControlGroup } from '@angular/common'; @Component({ selector: 'demo-form-sku-builder',template: ` <div class="ui raised segment"> <h2 class="ui header">Demo Form: Sku with Builder</h2> <form [ngFormModel]="myForm" (ngSubmit)="onSubmit(myForm.value)" class="ui form"> <div class="field"> <label for="skuInput">SKU</label> <input type="text" id="skuInput" placeholder="SKU" [ngFormControl]="myForm.controls['sku']"> </div> <button type="submit" class="ui button">Submit</button> </form> </div> ` }) export class DemoFormSkuBuilder { myForm: ControlGroup; constructor(fb: FormBuilder) { this.myForm = fb.group({ 'sku': ['ABC123'] }); } onSubmit(value: string): void { console.log('you submitted value: ',value); } }
let control = new Control('sku',Validators.required);
constructor(fb: FormBuilder) { this.myForm = fb.group({ 'sku': ['',Validators.required]

显示的将Control变量设置为实例变量

export class DemoFormWithValidationsExplicit { myForm: ControlGroup; sku: AbstractControl; constructor(fb: FormBuilder) { this.myForm = fb.group({ 'sku': ['',Validators.required] }); this.sku = this.myForm.controls['sku']; } onSubmit(value: string): void { console.log('you submitted value: ',value); } }

表单信息

<div *ngIf="!myForm.valid"

字段信息

[ngFormControl]="sku"> <div *ngIf="!sku.valid"

字段色彩

<div class="field" [class.error]="!sku.valid && sku.touched">

指定特定的校验器

class="ui error message">SKU is invalid</div> <div *ngIf="sku.hasError('required')"

所有代码

/* tslint:disable:no-string-literal */ import { Component } from '@angular/core'; import { CORE_DIRECTIVES,FORM_DIRECTIVES,ControlGroup,Validators,AbstractControl } from '@angular/common'; @Component({ selector: 'demo-form-with-validations-explicit',directives: [CORE_DIRECTIVES,FORM_DIRECTIVES],template: ` <div class="ui raised segment"> <h2 class="ui header">Demo Form: with validations (explicit)</h2> <form [ngFormModel]="myForm" (ngSubmit)="onSubmit(myForm.value)" class="ui form"> <div class="field" [class.error]="!sku.valid && sku.touched"> <label for="skuInput">SKU</label> <input type="text" id="skuInput" placeholder="SKU" [ngFormControl]="sku"> <div *ngIf="!sku.valid" class="ui error message">SKU is invalid</div> <div *ngIf="sku.hasError('required')" class="ui error message">SKU is required</div> </div> <div *ngIf="!myForm.valid" class="ui error message">Form is invalid</div> <button type="submit" class="ui button">Submit</button> </form> </div> ` }) export class DemoFormWithValidationsExplicit { myForm: ControlGroup; sku: AbstractControl; constructor(fb: FormBuilder) { this.myForm = fb.group({ 'sku': ['',Validators.required] }); this.sku = this.myForm.controls['sku']; } onSubmit(value: string): void { console.log('you submitted value: ',value); } }
export class DemoFormWithValidationsShorthand { myForm: ControlGroup; constructor(fb: FormBuilder) { this.myForm = fb.group({ 'sku': ['',Validators.required] }); } onSubmit(value: any): void { console.log('you submitted value:',value.sku); } }

声明1个本地sku援用

myForm.find

<div class="field" [class.error]="!myForm.find('sku').valid && myForm.find('sku').touched">

通过NgFormControl导出

<input type="text" id="skuInput" placeholder="SKU" #sku="ngForm" [ngFormControl]="myForm.controls['sku']">
<div *ngIf="!sku.control.valid" class="ui error message">SKU is invalid</div> <div *ngIf="sku.control.hasError('required')" class="ui error message">SKU is required</div>

sku变量的范围

// this won't work <div class="field" [class.error]="!sku.control.valid && sku.control.touched">

全部代码

import { Component } from '@angular/core'; import { CORE_DIRECTIVES,Validators } from '@angular/common'; @Component({ selector: 'demo-form-with-validations-shorthand',template: ` <div class="ui raised segment"> <h2 class="ui header">Demo Form: with validations (shorthand)</h2> <form [ngFormModel]="myForm" (ngSubmit)="onSubmit(myForm.value)" class="ui form"> <div class="field" [class.error]="!myForm.find('sku').valid && myForm.find('sku').touched"> <label for="skuInput">SKU</label> <input type="text" id="skuInput" placeholder="SKU" #sku="ngForm" [ngFormControl]="myForm.controls['sku']"> <div *ngIf="!sku.control.valid" class="ui error message">SKU is invalid</div> <div *ngIf="sku.control.hasError('required')" class="ui error message">SKU is required</div> </div> <div *ngIf="!myForm.valid" class="ui error message">Form is invalid</div> <button type="submit" class="ui button">Submit</button> </form> </div> ` }) export class DemoFormWithValidationsShorthand { myForm: ControlGroup; constructor(fb: FormBuilder) { this.myForm = fb.group({ 'sku': ['',Validators.required] }); } onSubmit(value: any): void { console.log('you submitted value:',value.sku); } }
export class Validators { static required(c: Control): StringMap<string,boolean> { return isBlank(c.value) || c.value == "" ? {"required": true} : null; }

编写1个校验器

function skuValidator(control: Control): { [s: string]: boolean } { if (!control.value.match(/^123/)) { return {invalidSku: true}; } }

分配校验器给Control

this.myForm = fb.group({ 'sku': ['',Validators.compose([ Validators.required,skuValidator])] });
<div *ngIf="sku.hasError('invalidSku')" class="ui error message">SKU must begin with <tt>123</tt></div>
export class DemoFormWithEvents { myForm: ControlGroup; sku: AbstractControl; constructor(fb: FormBuilder) { this.myForm = fb.group({ 'sku': ['',Validators.required] }); this.sku = this.myForm.controls['sku']; this.sku.valueChanges.subscribe( (value: string) => { console.log('sku changed to:',value); } ); this.myForm.valueChanges.subscribe( (form: any) => { console.log('form changed to:',form); } ); } onSubmit(form: any): void { console.log('you submitted value:',form.sku); } }
export class DemoFormNgModel { myForm: ControlGroup; productName: string; constructor(fb: FormBuilder) { this.myForm = fb.group({ 'productName': ['',Validators.required] }); } onSubmit(value: string): void { console.log('you submitted value: ',value); } }
<input type="text" id="productNameInput" placeholder="Product Name" [ngFormControl]="myForm.find('productName')" [(ngModel)]="productName">
<div class="ui info message"> The product name is: {{productName}} </div>
原创声明
本站部分文章基于互联网的整理,我们会把真正“有用/优质”的文章整理提供给各位开发者。本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
本文链接:http://www.jiecseo.com/news/show_68567.html
第五章 表单