From bcab06403c493a391a4eafd00311c8c0957fe840 Mon Sep 17 00:00:00 2001 From: Wenchen Li Date: Mon, 21 Nov 2016 15:59:00 -0500 Subject: [PATCH] Fix #2066 (#2800) --- .../forms/ts/app/hero-form.component.html | 15 +++++++------ .../forms/ts/app/hero-form.component.ts | 10 --------- public/docs/ts/latest/guide/forms.jade | 21 +++++-------------- 3 files changed, 12 insertions(+), 34 deletions(-) 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 4b90a9464f..0df80c1247 100644 --- a/public/docs/_examples/forms/ts/app/hero-form.component.html +++ b/public/docs/_examples/forms/ts/app/hero-form.component.html @@ -5,7 +5,7 @@

Hero Form

-
+
@@ -16,7 +16,7 @@ [(ngModel)]="model.name" name="name" #name="ngModel" > -
Name is required @@ -34,7 +34,7 @@ @@ -48,7 +48,7 @@ - + @@ -193,9 +193,9 @@ TODO: remove this: {{model.name}}
- - - + + +
TODO: remove this: {{spy.className}} -
diff --git a/public/docs/_examples/forms/ts/app/hero-form.component.ts b/public/docs/_examples/forms/ts/app/hero-form.component.ts index bc011fb4fd..b476f6651a 100644 --- a/public/docs/_examples/forms/ts/app/hero-form.component.ts +++ b/public/docs/_examples/forms/ts/app/hero-form.component.ts @@ -29,20 +29,10 @@ export class HeroFormComponent { // #enddocregion first // #docregion final - // Reset the form with a new hero AND restore 'pristine' class state - // by toggling 'active' flag which causes the form - // to be removed/re-added in a tick via NgIf - // TODO: Workaround until NgForm has a reset method (#6822) // #docregion new-hero - active = true; - // #docregion new-hero-v1 newHero() { this.model = new Hero(42, '', ''); - // #enddocregion new-hero-v1 - this.active = false; - setTimeout(() => this.active = true, 0); - // #docregion new-hero-v1 } // #enddocregion new-hero-v1 // #enddocregion new-hero diff --git a/public/docs/ts/latest/guide/forms.jade b/public/docs/ts/latest/guide/forms.jade index 8ce4250962..19e2a736f4 100644 --- a/public/docs/ts/latest/guide/forms.jade +++ b/public/docs/ts/latest/guide/forms.jade @@ -561,24 +561,13 @@ figure.image-display replacing the entire hero and clearing the `name` property programmatically. Angular makes no assumptions and leaves the control in its current, dirty state. :marked - We'll have to reset the form controls manually with a small trick. - We add an `active` flag to the component, initialized to `true`. When we add a new hero, - we toggle `active` false and then immediately back to true with a quick `setTimeout`. -+makeExample('forms/ts/app/hero-form.component.ts', - 'new-hero', - 'app/hero-form.component.ts (New Hero method - final)')(format=".") -:marked - Then we bind the form element to this `active` flag. + We'll have to reset the form controls. + We call the `reset()` method of the form after calling the `newHero()` method. +makeExample('forms/ts/app/hero-form.component.html', - 'form-active', - 'app/hero-form.component.html (Form tag)') + 'form-reset', + 'app/hero-form.component.html (Reset the form)') :marked - With `NgIf` bound to the `active` flag, - clicking "New Hero" removes the form from the DOM and recreates it in a blink of an eye. - The re-created form is in a pristine state. The error message is hidden. -.l-sub-section - :marked - This is a temporary workaround while we await a proper form reset feature. + This will reset the `heroForm` and its status by clicking "New Hero". :marked .l-main-section