diff --git a/public/docs/_examples/forms/ts/app/hero-form.component.html b/public/docs/_examples/forms/ts/app/hero-form.component.html index a44c9e9ee1..bf02bb195e 100644 --- a/public/docs/_examples/forms/ts/app/hero-form.component.html +++ b/public/docs/_examples/forms/ts/app/hero-form.component.html @@ -43,7 +43,7 @@
Form value: {{ heroForm.value | json }}
+ + + + diff --git a/public/docs/_examples/reactive-forms/ts/app/hero-detail-2.component.ts b/public/docs/_examples/reactive-forms/ts/app/hero-detail-2.component.ts new file mode 100644 index 0000000000..e3c0448a7f --- /dev/null +++ b/public/docs/_examples/reactive-forms/ts/app/hero-detail-2.component.ts @@ -0,0 +1,18 @@ +/* tslint:disable:component-class-suffix */ +// #docregion imports +import { Component } from '@angular/core'; +import { FormControl, FormGroup } from '@angular/forms'; +// #enddocregion imports + +@Component({ + moduleId: module.id, + selector: 'hero-detail-2', + templateUrl: './hero-detail-2.component.html' +}) +// #docregion v2 +export class HeroDetailComponent2 { + heroForm = new FormGroup ({ + name: new FormControl() + }); +} +// #enddocregion v2 diff --git a/public/docs/_examples/reactive-forms/ts/app/hero-detail-3.component.html b/public/docs/_examples/reactive-forms/ts/app/hero-detail-3.component.html new file mode 100644 index 0000000000..8edc544dd4 --- /dev/null +++ b/public/docs/_examples/reactive-forms/ts/app/hero-detail-3.component.html @@ -0,0 +1,16 @@ + +Form value: {{ heroForm.value | json }}
+Form status: {{ heroForm.status | json }}
+ diff --git a/public/docs/_examples/reactive-forms/ts/app/hero-detail-3.component.ts b/public/docs/_examples/reactive-forms/ts/app/hero-detail-3.component.ts new file mode 100644 index 0000000000..400c6911d6 --- /dev/null +++ b/public/docs/_examples/reactive-forms/ts/app/hero-detail-3.component.ts @@ -0,0 +1,28 @@ +/* tslint:disable:component-class-suffix */ +// #docregion imports +import { Component } from '@angular/core'; +import { FormBuilder, FormGroup, Validators } from '@angular/forms'; +// #enddocregion imports + +@Component({ + moduleId: module.id, + selector: 'hero-detail-3', + templateUrl: './hero-detail-3.component.html' +}) +// #docregion v3 +export class HeroDetailComponent3 { + heroForm: FormGroup; // <--- heroForm is of type FormGroup + + constructor(private fb: FormBuilder) { // <--- inject FormBuilder + this.createForm(); + } + + createForm() { + // #docregion required + this.heroForm = this.fb.group({ + name: ['', Validators.required ], + }); + // #enddocregion required + } +} +// #enddocregion v3 diff --git a/public/docs/_examples/reactive-forms/ts/app/hero-detail-3a.component.ts b/public/docs/_examples/reactive-forms/ts/app/hero-detail-3a.component.ts new file mode 100644 index 0000000000..b76803d7ed --- /dev/null +++ b/public/docs/_examples/reactive-forms/ts/app/hero-detail-3a.component.ts @@ -0,0 +1,26 @@ +/* tslint:disable:component-class-suffix */ +// #docregion imports +import { Component } from '@angular/core'; +import { FormBuilder, FormGroup } from '@angular/forms'; +// #enddocregion imports + +@Component({ + moduleId: module.id, + selector: 'hero-detail-3', + templateUrl: './hero-detail-3.component.html' +}) +// #docregion v3a +export class HeroDetailComponent3 { + heroForm: FormGroup; // <--- heroForm is of type FormGroup + + constructor(private fb: FormBuilder) { // <--- inject FormBuilder + this.createForm(); + } + + createForm() { + this.heroForm = this.fb.group({ + name: '', // <--- the FormControl called "name" + }); + } +} +// #enddocregion v3a diff --git a/public/docs/_examples/reactive-forms/ts/app/hero-detail-4.component.html b/public/docs/_examples/reactive-forms/ts/app/hero-detail-4.component.html new file mode 100644 index 0000000000..de8bb49e24 --- /dev/null +++ b/public/docs/_examples/reactive-forms/ts/app/hero-detail-4.component.html @@ -0,0 +1,46 @@ + +Form value: {{ heroForm.value | json }}
diff --git a/public/docs/_examples/reactive-forms/ts/app/hero-detail-4.component.ts b/public/docs/_examples/reactive-forms/ts/app/hero-detail-4.component.ts new file mode 100644 index 0000000000..8705765a56 --- /dev/null +++ b/public/docs/_examples/reactive-forms/ts/app/hero-detail-4.component.ts @@ -0,0 +1,35 @@ +/* tslint:disable:component-class-suffix */ +// #docregion imports +import { Component } from '@angular/core'; +import { FormBuilder, FormGroup, Validators } from '@angular/forms'; + +import { states } from './data-model'; +// #enddocregion imports + +@Component({ + moduleId: module.id, + selector: 'hero-detail-4', + templateUrl: './hero-detail-4.component.html' +}) +// #docregion v4 +export class HeroDetailComponent4 { + heroForm: FormGroup; + states = states; + + constructor(private fb: FormBuilder) { + this.createForm(); + } + + createForm() { + this.heroForm = this.fb.group({ + name: ['', Validators.required ], + street: '', + city: '', + state: '', + zip: '', + power: '', + sidekick: '' + }); + } +} +// #enddocregion v4 diff --git a/public/docs/_examples/reactive-forms/ts/app/hero-detail-5.component.html b/public/docs/_examples/reactive-forms/ts/app/hero-detail-5.component.html new file mode 100644 index 0000000000..078a263ae8 --- /dev/null +++ b/public/docs/_examples/reactive-forms/ts/app/hero-detail-5.component.html @@ -0,0 +1,56 @@ + + + +heroForm value: {{ heroForm.value | json}}
+Name value: {{ heroForm.get('name').value }}
+ + + +Street value: {{ heroForm.get('address.street').value}}
+ diff --git a/public/docs/_examples/reactive-forms/ts/app/hero-detail-5.component.ts b/public/docs/_examples/reactive-forms/ts/app/hero-detail-5.component.ts new file mode 100644 index 0000000000..2bafd0709e --- /dev/null +++ b/public/docs/_examples/reactive-forms/ts/app/hero-detail-5.component.ts @@ -0,0 +1,36 @@ +/* tslint:disable:component-class-suffix */ +import { Component } from '@angular/core'; +import { FormBuilder, FormGroup, Validators } from '@angular/forms'; + +import { states } from './data-model'; + +@Component({ + moduleId: module.id, + selector: 'hero-detail-5', + templateUrl: './hero-detail-5.component.html' +}) +// #docregion v5 +export class HeroDetailComponent5 { + heroForm: FormGroup; + states = states; + + constructor(private fb: FormBuilder) { + this.createForm(); + } + + createForm() { + this.heroForm = this.fb.group({ // <-- the parent FormGroup + name: ['', Validators.required ], + address: this.fb.group({ // <-- the child FormGroup + street: '', + city: '', + state: '', + zip: '' + }), + power: '', + sidekick: '' + }); + } +} +// #enddocregion v5 + diff --git a/public/docs/_examples/reactive-forms/ts/app/hero-detail-6.component.html b/public/docs/_examples/reactive-forms/ts/app/hero-detail-6.component.html new file mode 100644 index 0000000000..c075731f12 --- /dev/null +++ b/public/docs/_examples/reactive-forms/ts/app/hero-detail-6.component.html @@ -0,0 +1,46 @@ + +Form value: {{ heroForm.value | json }}
diff --git a/public/docs/_examples/reactive-forms/ts/app/hero-detail-6.component.ts b/public/docs/_examples/reactive-forms/ts/app/hero-detail-6.component.ts new file mode 100644 index 0000000000..9c592a953e --- /dev/null +++ b/public/docs/_examples/reactive-forms/ts/app/hero-detail-6.component.ts @@ -0,0 +1,59 @@ +/* tslint:disable:component-class-suffix */ +// #docregion import-input +import { Component, Input, OnChanges } from '@angular/core'; +// #enddocregion import-input +import { FormBuilder, FormGroup, Validators } from '@angular/forms'; + +// #docregion import-hero +import { Hero, states } from './data-model'; +// #enddocregion import-hero + +////////// 6 //////////////////// + +@Component({ + moduleId: module.id, + selector: 'hero-detail-6', + templateUrl: './hero-detail-5.component.html' +}) +// #docregion v6 +export class HeroDetailComponent6 implements OnChanges { + // #docregion hero + @Input() hero: Hero; + // #enddocregion hero + + heroForm: FormGroup; + states = states; + + constructor(private fb: FormBuilder) { + this.createForm(); + } + + createForm() { + // #docregion hero-form-model + this.heroForm = this.fb.group({ + name: ['', Validators.required ], + address: this.fb.group({ + street: '', + city: '', + state: '', + zip: '' + }), + power: '', + sidekick: '' + }); + // #enddocregion hero-form-model + } + + // #docregion patch-value-on-changes + ngOnChanges() { // <-- wrap patchValue in ngOnChanges + this.heroForm.reset(); + // #docregion patch-value + this.heroForm.patchValue({ + name: this.hero.name + }); + // #enddocregion patch-value + } + // #enddocregion patch-value-on-changes +} + +// #enddocregion v6 diff --git a/public/docs/_examples/reactive-forms/ts/app/hero-detail-7.component.html b/public/docs/_examples/reactive-forms/ts/app/hero-detail-7.component.html new file mode 100644 index 0000000000..ff24702b5a --- /dev/null +++ b/public/docs/_examples/reactive-forms/ts/app/hero-detail-7.component.html @@ -0,0 +1,46 @@ + +Form value: {{ heroForm.value | json }}
diff --git a/public/docs/_examples/reactive-forms/ts/app/hero-detail-7.component.ts b/public/docs/_examples/reactive-forms/ts/app/hero-detail-7.component.ts new file mode 100644 index 0000000000..60a220b6bd --- /dev/null +++ b/public/docs/_examples/reactive-forms/ts/app/hero-detail-7.component.ts @@ -0,0 +1,67 @@ +/* tslint:disable:component-class-suffix */ +// #docplaster +// #docregion imports +import { Component, Input, OnChanges } from '@angular/core'; +import { FormBuilder, FormGroup, Validators } from '@angular/forms'; + +import { Address, Hero, states } from './data-model'; +// #enddocregion imports + +@Component({ + moduleId: module.id, + selector: 'hero-detail-7', + templateUrl: './hero-detail-5.component.html' +}) +// #docregion v7 +export class HeroDetailComponent7 implements OnChanges { + @Input() hero: Hero; + + heroForm: FormGroup; + states = states; + + constructor(private fb: FormBuilder) { + this.createForm(); + } + + createForm() { + // #docregion address-form-group + this.heroForm = this.fb.group({ + name: ['', Validators.required ], + address: this.fb.group(new Address()), // <-- a FormGroup with a new address + power: '', + sidekick: '' + }); + // #enddocregion address-form-group + } + + // #docregion ngOnChanges + ngOnChanges() { + this.heroForm.reset({ + name: this.hero.name, + address: this.hero.addresses[0] || new Address() + }); + } + // #enddocregion ngOnChanges + + /* First version of ngOnChanges + // #docregion ngOnChanges-1 + ngOnChanges() + // #enddocregion ngOnChanges-1 + */ + ngOnChanges1() { + // #docregion reset + this.heroForm.reset(); + // #enddocregion reset + // #docregion ngOnChanges-1 + // #docregion set-value + this.heroForm.setValue({ + name: this.hero.name, + // #docregion set-value-address + address: this.hero.addresses[0] || new Address() + // #enddocregion set-value-address + }); + // #enddocregion set-value + } + // #enddocregion ngOnChanges-1 +} + diff --git a/public/docs/_examples/reactive-forms/ts/app/hero-detail-8.component.html b/public/docs/_examples/reactive-forms/ts/app/hero-detail-8.component.html new file mode 100644 index 0000000000..9993dd80ce --- /dev/null +++ b/public/docs/_examples/reactive-forms/ts/app/hero-detail-8.component.html @@ -0,0 +1,70 @@ + +heroForm value: {{ heroForm.value | json}}
diff --git a/public/docs/_examples/reactive-forms/ts/app/hero-detail-8.component.ts b/public/docs/_examples/reactive-forms/ts/app/hero-detail-8.component.ts new file mode 100644 index 0000000000..99cd6f822a --- /dev/null +++ b/public/docs/_examples/reactive-forms/ts/app/hero-detail-8.component.ts @@ -0,0 +1,69 @@ +/* tslint:disable:component-class-suffix */ +// #docregion imports +import { Component, Input, OnChanges } from '@angular/core'; +import { FormArray, FormBuilder, FormGroup, Validators } from '@angular/forms'; + +import { Address, Hero, states } from './data-model'; +// #enddocregion imports + +@Component({ + moduleId: module.id, + selector: 'hero-detail-8', + templateUrl: './hero-detail-8.component.html' +}) +// #docregion v8 +export class HeroDetailComponent8 implements OnChanges { + @Input() hero: Hero; + + heroForm: FormGroup; + states = states; + + // #docregion ctor + constructor(private fb: FormBuilder) { + this.createForm(); + this.logNameChange(); + } + // #enddocregion ctor + + createForm() { + // #docregion secretLairs-form-array + this.heroForm = this.fb.group({ + name: ['', Validators.required ], + secretLairs: this.fb.array([]), // <-- secretLairs as an empty FormArray + power: '', + sidekick: '' + }); + // #enddocregion secretLairs-form-array + } + + logNameChange() {/* Coming soon */} + + // #docregion onchanges + ngOnChanges() { + this.heroForm.reset({ + name: this.hero.name + }); + this.setAddresses(this.hero.addresses); + } + // #enddocregion onchanges + + // #docregion get-secret-lairs + get secretLairs(): FormArray { + return this.heroForm.get('secretLairs') as FormArray; + }; + // #enddocregion get-secret-lairs + + // #docregion set-addresses + setAddresses(addresses: Address[]) { + const addressFGs = addresses.map(address => this.fb.group(address)); + const addressFormArray = this.fb.array(addressFGs); + this.heroForm.setControl('secretLairs', addressFormArray); + } + // #enddocregion set-addresses + + // #docregion add-lair + addLair() { + this.secretLairs.push(this.fb.group(new Address())); + } + // #enddocregion add-lair +} diff --git a/public/docs/_examples/reactive-forms/ts/app/hero-detail.component.html b/public/docs/_examples/reactive-forms/ts/app/hero-detail.component.html new file mode 100644 index 0000000000..974867706f --- /dev/null +++ b/public/docs/_examples/reactive-forms/ts/app/hero-detail.component.html @@ -0,0 +1,73 @@ + + + + + + +heroForm value: {{ heroForm.value | json}}
+ + +