// 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 #f="ngForm" (ngSubmit)="onSubmit(f.value)" class="ui form">
</form>
<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);
}
}
export class DemoFormSkuBuilder { myForm: ControlGroup;
constructor(fb: FormBuilder) {
this.myForm = fb.group({
'sku': ['ABC123'] });
}
onSubmit(value: string): void {
console.log('you submitted value: ',value);
}
<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]
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);
}
}
<div class="field"
[class.error]="!myForm.find('sku').valid && myForm.find('sku').touched">
<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>
// 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;
}
function skuValidator(control: Control): { [s: string]: boolean } { if (!control.value.match(/^123/)) {
return {invalidSku: true};
}
}
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>
本站采用系统自动发货方式,付款后即出现下载入口,如有疑问请咨询在线客服!
售后时间:早10点 - 晚11:30点Copyright © 2024 jiecseo.com All rights reserved. 粤ICP备18085929号
欢迎光临【捷杰建站】,本站所有资源仅供学习与参考,禁止用于商业用途或从事违法行为!
技术营运:深圳市晟艺互动传媒有限公司