948 lines
28 KiB
HTML
948 lines
28 KiB
HTML
<html lang="en"><head></head><body><form id="mainForm" method="post" action="http://plnkr.co/edit/?p=preview" target="_self"><input type="hidden" name="files[app/app.component.ts]" value="import { Component } from '@angular/core';
|
|
|
|
@Component({
|
|
moduleId: module.id,
|
|
selector: 'my-app',
|
|
template: `
|
|
<div class="container">
|
|
<h1>Reactive Forms</h1>
|
|
<hero-list></hero-list>
|
|
</div>`
|
|
})
|
|
export class AppComponent { }
|
|
|
|
|
|
/*
|
|
Copyright 2016 Google Inc. All Rights Reserved.
|
|
Use of this source code is governed by an MIT-style license that
|
|
can be found in the LICENSE file at http://angular.io/license
|
|
*/"><input type="hidden" name="files[app/app.module.ts]" value="import { NgModule } from '@angular/core';
|
|
import { BrowserModule } from '@angular/platform-browser';
|
|
import { ReactiveFormsModule } from '@angular/forms'; // <-- #1 import module
|
|
|
|
import { AppComponent } from './app.component';
|
|
import { HeroDetailComponent } from './hero-detail.component'; // <-- #1 import component
|
|
import { HeroListComponent } from './hero-list.component';
|
|
|
|
import { HeroService } from './hero.service'; // <-- #1 import service
|
|
|
|
@NgModule({
|
|
imports: [
|
|
BrowserModule,
|
|
ReactiveFormsModule // <-- #2 add to Angular module imports
|
|
],
|
|
declarations: [
|
|
AppComponent,
|
|
HeroDetailComponent, // <-- #3 declare app component
|
|
HeroListComponent
|
|
],
|
|
providers: [ HeroService ], // <-- #4 provide HeroService
|
|
bootstrap: [ AppComponent ]
|
|
})
|
|
export class AppModule { }
|
|
|
|
|
|
/*
|
|
Copyright 2016 Google Inc. All Rights Reserved.
|
|
Use of this source code is governed by an MIT-style license that
|
|
can be found in the LICENSE file at http://angular.io/license
|
|
*/"><input type="hidden" name="files[app/data-model.ts]" value="export class Hero {
|
|
id = 0;
|
|
name = '';
|
|
addresses: Address[];
|
|
}
|
|
|
|
export class Address {
|
|
street = '';
|
|
city = '';
|
|
state = '';
|
|
zip = '';
|
|
}
|
|
|
|
export const heroes: Hero[] = [
|
|
{
|
|
id: 1,
|
|
name: 'Whirlwind',
|
|
addresses: [
|
|
{street: '123 Main', city: 'Anywhere', state: 'CA', zip: '94801'},
|
|
{street: '456 Maple', city: 'Somewhere', state: 'VA', zip: '23226'},
|
|
]
|
|
},
|
|
{
|
|
id: 2,
|
|
name: 'Bombastic',
|
|
addresses: [
|
|
{street: '789 Elm', city: 'Smallville', state: 'OH', zip: '04501'},
|
|
]
|
|
},
|
|
{
|
|
id: 3,
|
|
name: 'Magneta',
|
|
addresses: [ ]
|
|
},
|
|
];
|
|
|
|
export const states = ['CA', 'MD', 'OH', 'VA'];
|
|
|
|
|
|
/*
|
|
Copyright 2016 Google Inc. All Rights Reserved.
|
|
Use of this source code is governed by an MIT-style license that
|
|
can be found in the LICENSE file at http://angular.io/license
|
|
*/"><input type="hidden" name="files[app/demo.component.ts]" value="/* tslint:disable:member-ordering */
|
|
import { Component } from '@angular/core';
|
|
|
|
import { Hero } from './data-model';
|
|
import { HeroService } from './hero.service';
|
|
|
|
@Component({
|
|
moduleId: module.id,
|
|
selector: 'my-app',
|
|
templateUrl: 'demo.component.html'
|
|
})
|
|
export class DemoComponent {
|
|
|
|
demos: string[] = [
|
|
'Simple FormControl',
|
|
'Simple FormBuilder group',
|
|
'Group with multiple controls',
|
|
'Nested FormBuilder group',
|
|
'PatchValue',
|
|
'SetValue',
|
|
'FormArray',
|
|
'Final'].map(n => n + ' Demo');
|
|
|
|
final = this.demos.length;
|
|
demo = this.final; // current demo
|
|
|
|
heroes: Hero[];
|
|
selectedHero: Hero;
|
|
|
|
constructor(private heroService: HeroService) { }
|
|
|
|
getHeroes() {
|
|
return this.heroService.getHeroes().then(heroes => this.heroes = heroes);
|
|
}
|
|
|
|
select(hero: Hero) { this.selectedHero = hero; }
|
|
|
|
selectDemo(demo: number) {
|
|
this.demo = demo + 1;
|
|
this.heroes = undefined;
|
|
this.selectedHero = undefined;
|
|
this.getHeroes();
|
|
}
|
|
}
|
|
|
|
|
|
/*
|
|
Copyright 2016 Google Inc. All Rights Reserved.
|
|
Use of this source code is governed by an MIT-style license that
|
|
can be found in the LICENSE file at http://angular.io/license
|
|
*/"><input type="hidden" name="files[app/demo.module.ts]" value="import { NgModule } from '@angular/core';
|
|
import { BrowserModule } from '@angular/platform-browser';
|
|
import { ReactiveFormsModule } from '@angular/forms'; // <-- #1 import the module
|
|
|
|
import { DemoComponent } from './demo.component';
|
|
import { components } from './hero-detail-versions.component';
|
|
import { HeroDetailComponent } from './hero-detail.component';
|
|
import { HeroListComponent } from './hero-list.component';
|
|
|
|
import { HeroService } from './hero.service';
|
|
|
|
@NgModule({
|
|
imports: [
|
|
BrowserModule,
|
|
ReactiveFormsModule
|
|
],
|
|
declarations: [
|
|
DemoComponent, HeroDetailComponent, HeroListComponent,
|
|
...components
|
|
],
|
|
providers: [ HeroService ],
|
|
bootstrap: [ DemoComponent ]
|
|
})
|
|
export class DemoModule { }
|
|
|
|
|
|
/*
|
|
Copyright 2016 Google Inc. All Rights Reserved.
|
|
Use of this source code is governed by an MIT-style license that
|
|
can be found in the LICENSE file at http://angular.io/license
|
|
*/"><input type="hidden" name="files[app/hero-detail-versions.component.ts]" value="/* tslint:disable:component-class-suffix */
|
|
|
|
import { Component, Input, OnChanges } from '@angular/core';
|
|
import { FormArray, FormBuilder, FormControl, FormGroup } from '@angular/forms';
|
|
import { Address, Hero, states } from './data-model';
|
|
|
|
//////// 1 ////////////////////////
|
|
|
|
@Component({
|
|
moduleId: module.id,
|
|
selector: 'hero-detail-1',
|
|
templateUrl: './hero-detail-1.component.html'
|
|
})
|
|
|
|
export class HeroDetailComponent1 {
|
|
heroForm = new FormGroup ({
|
|
name: new FormControl()
|
|
});
|
|
}
|
|
|
|
//////// 2 ////////////////////////
|
|
|
|
@Component({
|
|
moduleId: module.id,
|
|
selector: 'hero-detail-2',
|
|
templateUrl: './hero-detail-2.component.html'
|
|
})
|
|
export class HeroDetailComponent2 {
|
|
heroForm: FormGroup; // <--- heroForm is of type FormGroup
|
|
constructor(private fb: FormBuilder) { // <--- inject FormBuilder
|
|
this.heroForm = this.fb.group({ // <--- make this.form a FormBuilder group
|
|
name: '', // <--- the FormControl called "name"
|
|
});
|
|
}
|
|
}
|
|
|
|
//////// 3 ////////////////////////
|
|
|
|
@Component({
|
|
moduleId: module.id,
|
|
selector: 'hero-detail-3',
|
|
templateUrl: './hero-detail-3.component.html'
|
|
})
|
|
export class HeroDetailComponent3 {
|
|
states = states;
|
|
heroForm: FormGroup;
|
|
constructor(private fb: FormBuilder) {
|
|
this.heroForm = this.fb.group({
|
|
name: '',
|
|
street: '',
|
|
city: '',
|
|
state: '',
|
|
zip: ''
|
|
});
|
|
}
|
|
}
|
|
|
|
////////// 4 /////////////////////
|
|
|
|
@Component({
|
|
moduleId: module.id,
|
|
selector: 'hero-detail-4',
|
|
templateUrl: './hero-detail-4.component.html'
|
|
})
|
|
export class HeroDetailComponent4 {
|
|
states = states;
|
|
heroForm: FormGroup;
|
|
constructor(private fb: FormBuilder) {
|
|
this.heroForm = this.fb.group({ // <-- the parent FormGroup
|
|
name: '',
|
|
address: this.fb.group({ // <-- the child FormGroup
|
|
street: '',
|
|
city: '',
|
|
state: '',
|
|
zip: ''
|
|
})
|
|
});
|
|
}
|
|
}
|
|
|
|
////////// 5 ////////////////////
|
|
|
|
@Component({
|
|
moduleId: module.id,
|
|
selector: 'hero-detail-5',
|
|
templateUrl: './hero-detail-4.component.html'
|
|
})
|
|
export class HeroDetailComponent5 implements OnChanges { // <-- implements OnChanges
|
|
@Input() hero: Hero;
|
|
|
|
states = states;
|
|
heroForm: FormGroup;
|
|
constructor(private fb: FormBuilder) {
|
|
this.heroForm = this.fb.group({
|
|
name: '',
|
|
address: this.fb.group({
|
|
street: '',
|
|
city: '',
|
|
state: '',
|
|
zip: ''
|
|
})
|
|
});
|
|
}
|
|
|
|
ngOnChanges() { // <-- wrap patchValue in ngOnChanges
|
|
this.heroForm.patchValue({
|
|
name: this.hero.name
|
|
});
|
|
}
|
|
}
|
|
|
|
|
|
///////// 6 ////////////////////
|
|
|
|
@Component({
|
|
moduleId: module.id,
|
|
selector: 'hero-detail-6',
|
|
templateUrl: './hero-detail-4.component.html'
|
|
})
|
|
export class HeroDetailComponent6 implements OnChanges {
|
|
@Input() hero: Hero;
|
|
|
|
states = states;
|
|
heroForm: FormGroup;
|
|
constructor(private fb: FormBuilder) {
|
|
this.heroForm = this.fb.group({
|
|
name: '',
|
|
address: this.fb.group(new Address()) // <-- a FormGroup with a new address
|
|
});
|
|
}
|
|
|
|
ngOnChanges() {
|
|
this.heroForm.reset();
|
|
this.heroForm.setValue({
|
|
name: this.hero.name,
|
|
address: this.hero.addresses[0] || new Address()
|
|
});
|
|
}
|
|
|
|
}
|
|
|
|
////////// 7 ////////////////////
|
|
|
|
@Component({
|
|
moduleId: module.id,
|
|
selector: 'hero-detail-7',
|
|
templateUrl: './hero-detail-5.component.html'
|
|
})
|
|
export class HeroDetailComponent7 implements OnChanges {
|
|
@Input() hero: Hero;
|
|
states = states;
|
|
heroForm: FormGroup;
|
|
constructor(private fb: FormBuilder) {
|
|
this.heroForm = this.fb.group({
|
|
name: '',
|
|
secretLairs: this.fb.array([]) // <-- secretLairs as an empty FormArray
|
|
});
|
|
}
|
|
|
|
get secretLairs(): FormArray {
|
|
return this.heroForm.get('secretLairs') as FormArray;
|
|
};
|
|
|
|
ngOnChanges() {
|
|
this.heroForm.reset();
|
|
this.heroForm.patchValue({
|
|
name: this.hero.name
|
|
});
|
|
this.setAddresses(this.hero.addresses);
|
|
}
|
|
setAddresses(addresses: Address[]) {
|
|
const addressFGs = addresses.map(address => this.fb.group(address));
|
|
const addressFormArray = this.fb.array(addressFGs);
|
|
this.heroForm.setControl('secretLairs', addressFormArray);
|
|
}
|
|
|
|
addLair() {
|
|
this.secretLairs.push(this.fb.group(new Address()));
|
|
}
|
|
}
|
|
|
|
////////////////////////////////
|
|
|
|
export const components = [
|
|
HeroDetailComponent1,
|
|
HeroDetailComponent2,
|
|
HeroDetailComponent3,
|
|
HeroDetailComponent4,
|
|
HeroDetailComponent5,
|
|
HeroDetailComponent6,
|
|
HeroDetailComponent7
|
|
];
|
|
|
|
|
|
/*
|
|
Copyright 2016 Google Inc. All Rights Reserved.
|
|
Use of this source code is governed by an MIT-style license that
|
|
can be found in the LICENSE file at http://angular.io/license
|
|
*/"><input type="hidden" name="files[app/hero-detail.component.ts]" value="// tslint:disable:no-unused-variable
|
|
import { Component, EventEmitter, Input, Output, OnChanges } from '@angular/core';
|
|
import { FormArray, FormBuilder, FormControl, FormGroup } from '@angular/forms';
|
|
import { Address, Hero, states } from './data-model';
|
|
|
|
@Component({
|
|
moduleId: module.id,
|
|
selector: 'hero-detail',
|
|
templateUrl: './hero-detail.component.html'
|
|
})
|
|
|
|
export class HeroDetailComponent implements OnChanges {
|
|
@Input() hero: Hero;
|
|
@Output() save = new EventEmitter<Hero>();
|
|
|
|
states = states;
|
|
heroForm: FormGroup;
|
|
|
|
constructor(private fb: FormBuilder) {
|
|
this.heroForm = this.fb.group({
|
|
name: '',
|
|
secretLairs: this.fb.array([])
|
|
});
|
|
}
|
|
|
|
get secretLairs(): FormArray {
|
|
return this.heroForm.get('secretLairs') as FormArray;
|
|
};
|
|
|
|
ngOnChanges() {
|
|
this.heroForm.reset();
|
|
this.heroForm.patchValue({
|
|
name: this.hero.name
|
|
});
|
|
this.setAddresses(this.hero.addresses);
|
|
}
|
|
|
|
setAddresses(addresses: Address[]) {
|
|
const addressFGs = addresses.map(address => this.fb.group(address));
|
|
const addressFormArray = this.fb.array(addressFGs);
|
|
this.heroForm.setControl('secretLairs', addressFormArray);
|
|
}
|
|
|
|
addLair() {
|
|
this.secretLairs.push(this.fb.group(new Address()));
|
|
}
|
|
|
|
onSubmit() {
|
|
const formModel = this.heroForm.value;
|
|
|
|
const newHero: Hero = {
|
|
id: this.hero.id,
|
|
name: formModel.name as string,
|
|
addresses: formModel.secretLairs
|
|
};
|
|
|
|
this.save.emit(newHero); // let parent decide what to do
|
|
|
|
console.log(newHero); // diagnostic
|
|
}
|
|
|
|
revert() { this.ngOnChanges(); }
|
|
}
|
|
|
|
|
|
/*
|
|
Copyright 2016 Google Inc. All Rights Reserved.
|
|
Use of this source code is governed by an MIT-style license that
|
|
can be found in the LICENSE file at http://angular.io/license
|
|
*/"><input type="hidden" name="files[app/hero-list.component.ts]" value="import { Component, OnInit } from '@angular/core';
|
|
|
|
import { Hero } from './data-model';
|
|
import { HeroService } from './hero.service';
|
|
|
|
@Component({
|
|
moduleId: module.id,
|
|
selector: 'hero-list',
|
|
templateUrl: 'hero-list.component.html'
|
|
})
|
|
export class HeroListComponent implements OnInit {
|
|
heroes: Hero[];
|
|
selectedHero: Hero;
|
|
|
|
constructor(private heroService: HeroService) { }
|
|
|
|
ngOnInit() { this.getHeroes(); }
|
|
|
|
getHeroes() {
|
|
this.heroService.getHeroes()
|
|
.then(heroes => this.heroes = heroes);
|
|
}
|
|
|
|
select(hero: Hero) { this.selectedHero = hero; }
|
|
|
|
onSave(hero: Hero) {
|
|
this.heroService.updateHero(hero)
|
|
.then(() => {
|
|
this.getHeroes();
|
|
const newHero = this.heroes.find(h => h === hero);
|
|
this.select(newHero);
|
|
});
|
|
}
|
|
}
|
|
|
|
|
|
/*
|
|
Copyright 2016 Google Inc. All Rights Reserved.
|
|
Use of this source code is governed by an MIT-style license that
|
|
can be found in the LICENSE file at http://angular.io/license
|
|
*/"><input type="hidden" name="files[app/hero.service.ts]" value="import { Injectable } from '@angular/core';
|
|
|
|
import { Hero, heroes } from './data-model';
|
|
|
|
@Injectable()
|
|
export class HeroService {
|
|
|
|
getHeroes(): Promise<Hero[]> {
|
|
return new Promise<Hero[]>(resolve => {
|
|
// simulate server latency with delay
|
|
setTimeout(() => resolve(heroes), 500);
|
|
});
|
|
}
|
|
|
|
updateHero(hero: Hero): Promise<Hero> {
|
|
// Demo: faking server update; nothing can go wrong ;-)
|
|
return new Promise<Hero>(resolve => {
|
|
// simulate server latency with delay
|
|
const ix = heroes.findIndex(h => h.id === hero.id);
|
|
setTimeout(() => {
|
|
heroes[ix] = hero;
|
|
resolve(hero);
|
|
}, 500);
|
|
});
|
|
}
|
|
}
|
|
|
|
|
|
/*
|
|
Copyright 2016 Google Inc. All Rights Reserved.
|
|
Use of this source code is governed by an MIT-style license that
|
|
can be found in the LICENSE file at http://angular.io/license
|
|
*/"><input type="hidden" name="files[app/main-final.ts]" value="// tslint:disable:no-unused-variable
|
|
import { platformBrowserDynamic } from '@angular/platform-browser-dynamic';
|
|
import { AppModule } from './app.module';
|
|
|
|
platformBrowserDynamic().bootstrapModule(AppModule);
|
|
|
|
|
|
/*
|
|
Copyright 2016 Google Inc. All Rights Reserved.
|
|
Use of this source code is governed by an MIT-style license that
|
|
can be found in the LICENSE file at http://angular.io/license
|
|
*/"><input type="hidden" name="files[app/main.ts]" value="// tslint:disable:no-unused-variable
|
|
import { platformBrowserDynamic } from '@angular/platform-browser-dynamic';
|
|
import { AppModule } from './app.module'; // just the final version
|
|
import { DemoModule } from './demo.module'; // demo picker
|
|
|
|
platformBrowserDynamic().bootstrapModule(DemoModule); // (AppModule);
|
|
|
|
|
|
/*
|
|
Copyright 2016 Google Inc. All Rights Reserved.
|
|
Use of this source code is governed by an MIT-style license that
|
|
can be found in the LICENSE file at http://angular.io/license
|
|
*/"><input type="hidden" name="files[styles.css]" value="/* Master Styles */
|
|
h1 {
|
|
color: #369;
|
|
font-family: Arial, Helvetica, sans-serif;
|
|
font-size: 250%;
|
|
}
|
|
h2, h3 {
|
|
color: #444;
|
|
font-family: Arial, Helvetica, sans-serif;
|
|
font-weight: lighter;
|
|
}
|
|
body {
|
|
margin: 2em;
|
|
}
|
|
body, input[text], button {
|
|
color: #888;
|
|
font-family: Cambria, Georgia;
|
|
}
|
|
a {
|
|
cursor: pointer;
|
|
cursor: hand;
|
|
}
|
|
button {
|
|
font-family: Arial;
|
|
background-color: #eee;
|
|
border: none;
|
|
padding: 5px 10px;
|
|
border-radius: 4px;
|
|
cursor: pointer;
|
|
cursor: hand;
|
|
}
|
|
button:hover {
|
|
background-color: #cfd8dc;
|
|
}
|
|
button:disabled {
|
|
background-color: #eee;
|
|
color: #aaa;
|
|
cursor: auto;
|
|
}
|
|
|
|
/* Navigation link styles */
|
|
nav a {
|
|
padding: 5px 10px;
|
|
text-decoration: none;
|
|
margin-right: 10px;
|
|
margin-top: 10px;
|
|
display: inline-block;
|
|
background-color: #eee;
|
|
border-radius: 4px;
|
|
}
|
|
nav a:visited, a:link {
|
|
color: #607D8B;
|
|
}
|
|
nav a:hover {
|
|
color: #039be5;
|
|
background-color: #CFD8DC;
|
|
}
|
|
nav a.active {
|
|
color: #039be5;
|
|
}
|
|
|
|
/* items class */
|
|
.items {
|
|
margin: 0 0 2em 0;
|
|
list-style-type: none;
|
|
padding: 0;
|
|
width: 24em;
|
|
}
|
|
.items li {
|
|
cursor: pointer;
|
|
position: relative;
|
|
left: 0;
|
|
background-color: #EEE;
|
|
margin: .5em;
|
|
padding: .3em 0;
|
|
height: 1.6em;
|
|
border-radius: 4px;
|
|
}
|
|
.items li:hover {
|
|
color: #607D8B;
|
|
background-color: #DDD;
|
|
left: .1em;
|
|
}
|
|
.items li.selected {
|
|
background-color: #CFD8DC;
|
|
color: white;
|
|
}
|
|
.items li.selected:hover {
|
|
background-color: #BBD8DC;
|
|
}
|
|
.items .text {
|
|
position: relative;
|
|
top: -3px;
|
|
}
|
|
.items .badge {
|
|
display: inline-block;
|
|
font-size: small;
|
|
color: white;
|
|
padding: 0.8em 0.7em 0 0.7em;
|
|
background-color: #607D8B;
|
|
line-height: 1em;
|
|
position: relative;
|
|
left: -1px;
|
|
top: -4px;
|
|
height: 1.8em;
|
|
margin-right: .8em;
|
|
border-radius: 4px 0 0 4px;
|
|
}
|
|
/* everywhere else */
|
|
* {
|
|
font-family: Arial, Helvetica, sans-serif;
|
|
}
|
|
|
|
|
|
/*
|
|
Copyright 2016 Google Inc. All Rights Reserved.
|
|
Use of this source code is governed by an MIT-style license that
|
|
can be found in the LICENSE file at http://angular.io/license
|
|
*/"><input type="hidden" name="files[app/demo.component.html]" value="<div class="container">
|
|
<h1>Reactive Forms</h1>
|
|
<h4><i>Pick a demo:</i>
|
|
<select [selectedIndex]="demo - 1" (change)="selectDemo($event.target.selectedIndex)">
|
|
<option *ngFor="let demo of demos">{{demo}}</option>
|
|
</select>
|
|
</h4>
|
|
|
|
<hr>
|
|
|
|
<div class="demo">
|
|
<hero-list *ngIf="demo===final"></hero-list>
|
|
|
|
<hero-detail-1 *ngIf="demo===1"></hero-detail-1>
|
|
<hero-detail-2 *ngIf="demo===2"></hero-detail-2>
|
|
<hero-detail-3 *ngIf="demo===3"></hero-detail-3>
|
|
<hero-detail-4 *ngIf="demo===4"></hero-detail-4>
|
|
|
|
<div *ngIf="demo >= 5 && demo !== final" >
|
|
|
|
<p *ngIf="!heroes"><b><i>Loading heroes ... </i></b></p>
|
|
<h3 *ngIf="heroes">Select a hero:</h3>
|
|
|
|
<nav>
|
|
<a *ngFor="let hero of heroes" (click)="select(hero)">{{hero.name}}</a>
|
|
</nav>
|
|
|
|
<div *ngIf="selectedHero">
|
|
<hr>
|
|
<h2>Hero Detail</h2>
|
|
<h3>Editing: {{selectedHero.name}}</h3>
|
|
<hero-detail-5 [hero]=selectedHero *ngIf="demo===5"></hero-detail-5>
|
|
<hero-detail-6 [hero]=selectedHero *ngIf="demo===6"></hero-detail-6>
|
|
<hero-detail-7 [hero]=selectedHero *ngIf="demo===7"></hero-detail-7>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
|
|
|
|
<!--
|
|
Copyright 2016 Google Inc. All Rights Reserved.
|
|
Use of this source code is governed by an MIT-style license that
|
|
can be found in the LICENSE file at http://angular.io/license
|
|
-->"><input type="hidden" name="files[app/hero-detail-1.component.html]" value="<h2>Hero Detail</h2>
|
|
<h3><i>A simple form with a single FormControl</i></h3>
|
|
<form novalidate [formGroup]="heroForm">
|
|
<div class="form-group">
|
|
<label>Name:</label>
|
|
<input class="form-control" formControlName="name">
|
|
</div>
|
|
</form>
|
|
|
|
|
|
<!--
|
|
Copyright 2016 Google Inc. All Rights Reserved.
|
|
Use of this source code is governed by an MIT-style license that
|
|
can be found in the LICENSE file at http://angular.io/license
|
|
-->"><input type="hidden" name="files[app/hero-detail-2.component.html]" value="<h2>Hero Detail</h2>
|
|
<h3><i>A simple form with a single FormControl using FormBuilder</i></h3>
|
|
<form novalidate [formGroup]="heroForm">
|
|
<div class="form-group">
|
|
<label>Name:</label>
|
|
<input class="form-control" formControlName="name">
|
|
</div>
|
|
</form>
|
|
|
|
<p>Form: {{ heroForm.value | json }}</p>
|
|
|
|
|
|
<!--
|
|
Copyright 2016 Google Inc. All Rights Reserved.
|
|
Use of this source code is governed by an MIT-style license that
|
|
can be found in the LICENSE file at http://angular.io/license
|
|
-->"><input type="hidden" name="files[app/hero-detail-3.component.html]" value="<h2>Hero Detail</h2>
|
|
<h3><i>A simple form with multiple FormControls in a single FormBuilder group</i></h3>
|
|
<form novalidate [formGroup]="heroForm">
|
|
<div class="form-group">
|
|
<label>Name:</label>
|
|
<input class="form-control" formControlName="name">
|
|
</div>
|
|
<div class="form-group">
|
|
<label>Street:</label>
|
|
<input class="form-control" formControlName="street">
|
|
</div>
|
|
<div class="form-group">
|
|
<label>City:</label>
|
|
<input class="form-control" formControlName="city">
|
|
</div>
|
|
<div class="form-group">
|
|
<label>State:</label>
|
|
<select class="form-control" formControlName="state">
|
|
<option *ngFor="let state of states">{{state}}</option>
|
|
</select>
|
|
</div>
|
|
<div class="form-group">
|
|
<label>Zip Code:</label>
|
|
<input class="form-control" formControlName="zip">
|
|
</div>
|
|
</form>
|
|
|
|
|
|
<!--
|
|
Copyright 2016 Google Inc. All Rights Reserved.
|
|
Use of this source code is governed by an MIT-style license that
|
|
can be found in the LICENSE file at http://angular.io/license
|
|
-->"><input type="hidden" name="files[app/hero-detail-4.component.html]" value="<form novalidate [formGroup]="heroForm">
|
|
<div class="form-group">
|
|
<label>Name:</label>
|
|
<input class="form-control" formControlName="name">
|
|
</div>
|
|
<div formGroupName="address" class="well well-lg">
|
|
<h4>Secret Lair</h4>
|
|
<div class="form-group">
|
|
<label>Street:</label>
|
|
<input class="form-control" formControlName="street">
|
|
</div>
|
|
<div class="form-group">
|
|
<label>City:</label>
|
|
<input class="form-control" formControlName="city">
|
|
</div>
|
|
<div class="form-group">
|
|
<label>State:</label>
|
|
<select class="form-control" formControlName="state">
|
|
<option *ngFor="let state of states">{{state}}</option>
|
|
</select>
|
|
</div>
|
|
<div class="form-group">
|
|
<label>Zip Code:</label>
|
|
<input class="form-control" formControlName="zip">
|
|
</div>
|
|
</div>
|
|
</form>
|
|
|
|
<p>heroForm value: {{ heroForm.value | json}}</p>
|
|
<h4>Extra info for the curious:</h4>
|
|
<p>Name value: {{ heroForm.get('name').value }}</p>
|
|
|
|
<p>Street value: {{ heroForm.get('address.street').value}}</p>
|
|
|
|
|
|
<!--
|
|
Copyright 2016 Google Inc. All Rights Reserved.
|
|
Use of this source code is governed by an MIT-style license that
|
|
can be found in the LICENSE file at http://angular.io/license
|
|
-->"><input type="hidden" name="files[app/hero-detail-5.component.html]" value="<h3><i>Using FormArray to add groups</i></h3>
|
|
|
|
<form novalidate [formGroup]="heroForm" (ngSubmit)="onSubmit(heroForm)">
|
|
<p>Form Changed: {{ heroForm.dirty }}</p>
|
|
|
|
<div class="form-group">
|
|
<label>Name:</label>
|
|
<input class="form-control" formControlName="name">
|
|
</div>
|
|
<div formArrayName="secretLairs" class="well well-lg">
|
|
<div *ngFor="let address of secretLairs.controls; let i=index" [formGroupName]="i" >
|
|
<!-- The repeated address template -->
|
|
<h4>Address #{{i + 1}}</h4>
|
|
<div style="margin-left: 1em;">
|
|
<div class="form-group">
|
|
<label>Street:</label>
|
|
<input class="form-control" formControlName="street">
|
|
</div>
|
|
<div class="form-group">
|
|
<label>City:</label>
|
|
<input class="form-control" formControlName="city">
|
|
</div>
|
|
<div class="form-group">
|
|
<label>State:</label>
|
|
<select class="form-control" formControlName="state">
|
|
<option *ngFor="let state of states">{{state}}</option>
|
|
</select>
|
|
</div>
|
|
<div class="form-group">
|
|
<label>Zip Code:</label>
|
|
<input class="form-control" formControlName="zip">
|
|
</div>
|
|
</div>
|
|
<br>
|
|
<!-- End of the repeated address template -->
|
|
</div>
|
|
<button (click)="addLair()">Add a Secret Lair</button> <!-- add button -->
|
|
</div>
|
|
</form>
|
|
|
|
<p>heroForm value: {{ heroForm.value | json}}</p>
|
|
|
|
|
|
<!--
|
|
Copyright 2016 Google Inc. All Rights Reserved.
|
|
Use of this source code is governed by an MIT-style license that
|
|
can be found in the LICENSE file at http://angular.io/license
|
|
-->"><input type="hidden" name="files[app/hero-detail.component.html]" value="<form novalidate [formGroup]="heroForm" (ngSubmit)="onSubmit(heroForm)">
|
|
<div style="margin-bottom: 1em">
|
|
<button type="submit"
|
|
[disabled]="!heroForm.dirty" class="btn btn-success">Save</button> &nbsp;
|
|
<button type="reset" (click)="revert()"
|
|
[disabled]="!heroForm.dirty" class="btn btn-danger">Revert</button>
|
|
</div>
|
|
|
|
<div class="form-group">
|
|
<label>Name:</label>
|
|
<input class="form-control" formControlName="name">
|
|
</div>
|
|
|
|
<div formArrayName="secretLairs" class="well well-lg">
|
|
<div *ngFor="let address of secretLairs.controls; let i=index" [formGroupName]="i" >
|
|
<!-- The repeated address template -->
|
|
<h4>Address #{{i + 1}}</h4>
|
|
<div style="margin-left: 1em;">
|
|
<div class="form-group">
|
|
<label>Street:</label>
|
|
<input class="form-control" formControlName="street">
|
|
</div>
|
|
<div class="form-group">
|
|
<label>City:</label>
|
|
<input class="form-control" formControlName="city">
|
|
</div>
|
|
<div class="form-group">
|
|
<label>State:</label>
|
|
<select class="form-control" formControlName="state">
|
|
<option *ngFor="let state of states">{{state}}</option>
|
|
</select>
|
|
</div>
|
|
<div class="form-group">
|
|
<label>Zip Code:</label>
|
|
<input class="form-control" formControlName="zip">
|
|
</div>
|
|
</div>
|
|
<br>
|
|
<!-- End of the repeated address template -->
|
|
</div>
|
|
<button (click)="addLair()">Add a Secret Lair</button> <!-- add button -->
|
|
</div>
|
|
</form>
|
|
|
|
<p>heroForm value: {{ heroForm.value | json}}</p>
|
|
|
|
|
|
<!--
|
|
Copyright 2016 Google Inc. All Rights Reserved.
|
|
Use of this source code is governed by an MIT-style license that
|
|
can be found in the LICENSE file at http://angular.io/license
|
|
-->"><input type="hidden" name="files[app/hero-list.component.html]" value="<p *ngIf="!heroes"><b><i>Loading heroes ... </i></b></p>
|
|
<h3 *ngIf="heroes">Select a hero:</h3>
|
|
|
|
<nav>
|
|
<a *ngFor="let hero of heroes" (click)="select(hero)">{{hero.name}}</a>
|
|
</nav>
|
|
|
|
<div *ngIf="selectedHero">
|
|
<hr>
|
|
<h2>Hero Detail</h2>
|
|
<h3>Editing: {{selectedHero.name}}</h3>
|
|
<hero-detail [hero]="selectedHero" (save)="onSave($event)"></hero-detail>
|
|
</div>
|
|
|
|
|
|
<!--
|
|
Copyright 2016 Google Inc. All Rights Reserved.
|
|
Use of this source code is governed by an MIT-style license that
|
|
can be found in the LICENSE file at http://angular.io/license
|
|
-->"><input type="hidden" name="files[index.html]" value="<!DOCTYPE html>
|
|
<html>
|
|
<head>
|
|
<title>Hero Form</title>
|
|
<meta charset="UTF-8">
|
|
<meta name="viewport" content="width=device-width, initial-scale=1">
|
|
|
|
<link rel="stylesheet" href="styles.css">
|
|
<link rel="stylesheet" href="https://unpkg.com/bootstrap@3.3.7/dist/css/bootstrap.min.css">
|
|
|
|
<!-- Polyfills for older browsers -->
|
|
<script src="https://unpkg.com/core-js/client/shim.min.js"></script>
|
|
|
|
<script src="https://unpkg.com/zone.js@0.7.2?main=browser"></script>
|
|
<script src="https://unpkg.com/reflect-metadata@0.1.8"></script>
|
|
<script src="https://unpkg.com/systemjs@0.19.39/dist/system.src.js"></script>
|
|
|
|
<script src="https://cdn.rawgit.com/angular/angular.io/b3c65a9/public/docs/_examples/_boilerplate/systemjs.config.web.js"></script>
|
|
<script>
|
|
System.import('app').catch(function(err){ console.error(err); });
|
|
</script>
|
|
</head>
|
|
|
|
<body>
|
|
<my-app>Loading...</my-app>
|
|
</body>
|
|
|
|
</html>
|
|
|
|
|
|
<!--
|
|
Copyright 2016 Google Inc. All Rights Reserved.
|
|
Use of this source code is governed by an MIT-style license that
|
|
can be found in the LICENSE file at http://angular.io/license
|
|
-->"><input type="hidden" name="tags[0]" value="angular"><input type="hidden" name="tags[1]" value="example"><input type="hidden" name="tags[2]" value="reactive"><input type="hidden" name="tags[3]" value="forms"><input type="hidden" name="private" value="true"><input type="hidden" name="description" value="Angular Example - Reactive Forms (final)"></form><script>document.getElementById("mainForm").submit();</script></body></html> |