Skip to content
This repository was archived by the owner on Dec 4, 2017. It is now read-only.

Commit bcab064

Browse files
Wenchen Liwardbell
Wenchen Li
authored andcommitted
Fix #2066 (#2800)
1 parent 694b58d commit bcab064

File tree

3 files changed

+12
-34
lines changed

3 files changed

+12
-34
lines changed

public/docs/_examples/forms/ts/app/hero-form.component.html

+7-8
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
<div [hidden]="submitted">
66
<h1>Hero Form</h1>
77
<!-- #docregion ngSubmit -->
8-
<form *ngIf="active" (ngSubmit)="onSubmit()" #heroForm="ngForm">
8+
<form (ngSubmit)="onSubmit()" #heroForm="ngForm">
99
<!-- #enddocregion ngSubmit -->
1010
<!-- #enddocregion edit-div -->
1111
<div class="form-group">
@@ -16,7 +16,7 @@ <h1>Hero Form</h1>
1616
[(ngModel)]="model.name" name="name"
1717
#name="ngModel" >
1818
<!-- #docregion hidden-error-msg -->
19-
<div [hidden]="name.valid || name.pristine"
19+
<div [hidden]="name.valid || name.pristine"
2020
class="alert alert-danger">
2121
<!-- #enddocregion hidden-error-msg -->
2222
Name is required
@@ -34,7 +34,7 @@ <h1>Hero Form</h1>
3434
<label for="power">Hero Power</label>
3535
<select class="form-control" id="power"
3636
required
37-
[(ngModel)]="model.power" name="power"
37+
[(ngModel)]="model.power" name="power"
3838
#power="ngModel" >
3939
<option *ngFor="let p of powers" [value]="p">{{p}}</option>
4040
</select>
@@ -48,7 +48,7 @@ <h1>Hero Form</h1>
4848
<!-- #enddocregion submit-button -->
4949

5050
<!-- #docregion new-hero-button -->
51-
<button type="button" class="btn btn-default" (click)="newHero()">New Hero</button>
51+
<button type="button" class="btn btn-default" (click)="newHero(); heroForm.reset()">New Hero</button>
5252
<!-- #enddocregion new-hero-button -->
5353

5454
<!-- #enddocregion final -->
@@ -193,9 +193,9 @@ <h1>Hero Form</h1>
193193
TODO: remove this: {{model.name}}
194194
<!-- #enddocregion ngModel-3-->
195195
<hr>
196-
<!-- #docregion form-active -->
197-
<form *ngIf="active">
198-
<!-- #enddocregion form-active -->
196+
<!-- #docregion form-reset -->
197+
<button type="button" class="btn btn-default" (click)="newHero(); heroForm.reset()">New Hero</button>
198+
<!-- #enddocregion form-reset -->
199199

200200
<!-- #docregion ngModelName-1 -->
201201
<input type="text" class="form-control" id="name"
@@ -210,6 +210,5 @@ <h1>Hero Form</h1>
210210
#spy >
211211
<br>TODO: remove this: {{spy.className}}
212212
<!-- #enddocregion ngModelName-2 -->
213-
</form>
214213

215214
</div>

public/docs/_examples/forms/ts/app/hero-form.component.ts

-10
Original file line numberDiff line numberDiff line change
@@ -29,20 +29,10 @@ export class HeroFormComponent {
2929
// #enddocregion first
3030

3131
// #docregion final
32-
// Reset the form with a new hero AND restore 'pristine' class state
33-
// by toggling 'active' flag which causes the form
34-
// to be removed/re-added in a tick via NgIf
35-
// TODO: Workaround until NgForm has a reset method (#6822)
3632
// #docregion new-hero
37-
active = true;
38-
3933
// #docregion new-hero-v1
4034
newHero() {
4135
this.model = new Hero(42, '', '');
42-
// #enddocregion new-hero-v1
43-
this.active = false;
44-
setTimeout(() => this.active = true, 0);
45-
// #docregion new-hero-v1
4636
}
4737
// #enddocregion new-hero-v1
4838
// #enddocregion new-hero

public/docs/ts/latest/guide/forms.jade

+5-16
Original file line numberDiff line numberDiff line change
@@ -561,24 +561,13 @@ figure.image-display
561561
replacing the entire hero and clearing the `name` property programmatically.
562562
Angular makes no assumptions and leaves the control in its current, dirty state.
563563
:marked
564-
We'll have to reset the form controls manually with a small trick.
565-
We add an `active` flag to the component, initialized to `true`. When we add a new hero,
566-
we toggle `active` false and then immediately back to true with a quick `setTimeout`.
567-
+makeExample('forms/ts/app/hero-form.component.ts',
568-
'new-hero',
569-
'app/hero-form.component.ts (New Hero method - final)')(format=".")
570-
:marked
571-
Then we bind the form element to this `active` flag.
564+
We'll have to reset the form controls.
565+
We call the `reset()` method of the form after calling the `newHero()` method.
572566
+makeExample('forms/ts/app/hero-form.component.html',
573-
'form-active',
574-
'app/hero-form.component.html (Form tag)')
567+
'form-reset',
568+
'app/hero-form.component.html (Reset the form)')
575569
:marked
576-
With `NgIf` bound to the `active` flag,
577-
clicking "New Hero" removes the form from the DOM and recreates it in a blink of an eye.
578-
The re-created form is in a pristine state. The error message is hidden.
579-
.l-sub-section
580-
:marked
581-
This is a temporary workaround while we await a proper form reset feature.
570+
This will reset the `heroForm` and its status by clicking "New Hero".
582571
:marked
583572

584573
.l-main-section

0 commit comments

Comments
 (0)