parent
694b58d06e
commit
bcab06403c
|
@ -5,7 +5,7 @@
|
||||||
<div [hidden]="submitted">
|
<div [hidden]="submitted">
|
||||||
<h1>Hero Form</h1>
|
<h1>Hero Form</h1>
|
||||||
<!-- #docregion ngSubmit -->
|
<!-- #docregion ngSubmit -->
|
||||||
<form *ngIf="active" (ngSubmit)="onSubmit()" #heroForm="ngForm">
|
<form (ngSubmit)="onSubmit()" #heroForm="ngForm">
|
||||||
<!-- #enddocregion ngSubmit -->
|
<!-- #enddocregion ngSubmit -->
|
||||||
<!-- #enddocregion edit-div -->
|
<!-- #enddocregion edit-div -->
|
||||||
<div class="form-group">
|
<div class="form-group">
|
||||||
|
@ -16,7 +16,7 @@
|
||||||
[(ngModel)]="model.name" name="name"
|
[(ngModel)]="model.name" name="name"
|
||||||
#name="ngModel" >
|
#name="ngModel" >
|
||||||
<!-- #docregion hidden-error-msg -->
|
<!-- #docregion hidden-error-msg -->
|
||||||
<div [hidden]="name.valid || name.pristine"
|
<div [hidden]="name.valid || name.pristine"
|
||||||
class="alert alert-danger">
|
class="alert alert-danger">
|
||||||
<!-- #enddocregion hidden-error-msg -->
|
<!-- #enddocregion hidden-error-msg -->
|
||||||
Name is required
|
Name is required
|
||||||
|
@ -34,7 +34,7 @@
|
||||||
<label for="power">Hero Power</label>
|
<label for="power">Hero Power</label>
|
||||||
<select class="form-control" id="power"
|
<select class="form-control" id="power"
|
||||||
required
|
required
|
||||||
[(ngModel)]="model.power" name="power"
|
[(ngModel)]="model.power" name="power"
|
||||||
#power="ngModel" >
|
#power="ngModel" >
|
||||||
<option *ngFor="let p of powers" [value]="p">{{p}}</option>
|
<option *ngFor="let p of powers" [value]="p">{{p}}</option>
|
||||||
</select>
|
</select>
|
||||||
|
@ -48,7 +48,7 @@
|
||||||
<!-- #enddocregion submit-button -->
|
<!-- #enddocregion submit-button -->
|
||||||
|
|
||||||
<!-- #docregion new-hero-button -->
|
<!-- #docregion new-hero-button -->
|
||||||
<button type="button" class="btn btn-default" (click)="newHero()">New Hero</button>
|
<button type="button" class="btn btn-default" (click)="newHero(); heroForm.reset()">New Hero</button>
|
||||||
<!-- #enddocregion new-hero-button -->
|
<!-- #enddocregion new-hero-button -->
|
||||||
|
|
||||||
<!-- #enddocregion final -->
|
<!-- #enddocregion final -->
|
||||||
|
@ -193,9 +193,9 @@
|
||||||
TODO: remove this: {{model.name}}
|
TODO: remove this: {{model.name}}
|
||||||
<!-- #enddocregion ngModel-3-->
|
<!-- #enddocregion ngModel-3-->
|
||||||
<hr>
|
<hr>
|
||||||
<!-- #docregion form-active -->
|
<!-- #docregion form-reset -->
|
||||||
<form *ngIf="active">
|
<button type="button" class="btn btn-default" (click)="newHero(); heroForm.reset()">New Hero</button>
|
||||||
<!-- #enddocregion form-active -->
|
<!-- #enddocregion form-reset -->
|
||||||
|
|
||||||
<!-- #docregion ngModelName-1 -->
|
<!-- #docregion ngModelName-1 -->
|
||||||
<input type="text" class="form-control" id="name"
|
<input type="text" class="form-control" id="name"
|
||||||
|
@ -210,6 +210,5 @@
|
||||||
#spy >
|
#spy >
|
||||||
<br>TODO: remove this: {{spy.className}}
|
<br>TODO: remove this: {{spy.className}}
|
||||||
<!-- #enddocregion ngModelName-2 -->
|
<!-- #enddocregion ngModelName-2 -->
|
||||||
</form>
|
|
||||||
|
|
||||||
</div>
|
</div>
|
||||||
|
|
|
@ -29,20 +29,10 @@ export class HeroFormComponent {
|
||||||
// #enddocregion first
|
// #enddocregion first
|
||||||
|
|
||||||
// #docregion final
|
// #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
|
// #docregion new-hero
|
||||||
active = true;
|
|
||||||
|
|
||||||
// #docregion new-hero-v1
|
// #docregion new-hero-v1
|
||||||
newHero() {
|
newHero() {
|
||||||
this.model = new Hero(42, '', '');
|
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-v1
|
||||||
// #enddocregion new-hero
|
// #enddocregion new-hero
|
||||||
|
|
|
@ -561,24 +561,13 @@ figure.image-display
|
||||||
replacing the entire hero and clearing the `name` property programmatically.
|
replacing the entire hero and clearing the `name` property programmatically.
|
||||||
Angular makes no assumptions and leaves the control in its current, dirty state.
|
Angular makes no assumptions and leaves the control in its current, dirty state.
|
||||||
:marked
|
:marked
|
||||||
We'll have to reset the form controls manually with a small trick.
|
We'll have to reset the form controls.
|
||||||
We add an `active` flag to the component, initialized to `true`. When we add a new hero,
|
We call the `reset()` method of the form after calling the `newHero()` method.
|
||||||
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.
|
|
||||||
+makeExample('forms/ts/app/hero-form.component.html',
|
+makeExample('forms/ts/app/hero-form.component.html',
|
||||||
'form-active',
|
'form-reset',
|
||||||
'app/hero-form.component.html (Form tag)')
|
'app/hero-form.component.html (Reset the form)')
|
||||||
:marked
|
:marked
|
||||||
With `NgIf` bound to the `active` flag,
|
This will reset the `heroForm` and its status by clicking "New Hero".
|
||||||
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.
|
|
||||||
:marked
|
:marked
|
||||||
|
|
||||||
.l-main-section
|
.l-main-section
|
||||||
|
|
Loading…
Reference in New Issue