refactor: move angular source to /packages rather than modules/@angular
This commit is contained in:
@ -0,0 +1,43 @@
|
||||
/**
|
||||
* @license
|
||||
* Copyright 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 https://angular.io/license
|
||||
*/
|
||||
|
||||
import {ElementArrayFinder, browser, by, element} from 'protractor';
|
||||
import {verifyNoBrowserErrors} from '../../../../_common/e2e_util';
|
||||
|
||||
describe('nestedFormArray example', () => {
|
||||
afterEach(verifyNoBrowserErrors);
|
||||
let inputs: ElementArrayFinder;
|
||||
let buttons: ElementArrayFinder;
|
||||
|
||||
beforeEach(() => {
|
||||
browser.get('/forms/ts/nestedFormArray/index.html');
|
||||
inputs = element.all(by.css('input'));
|
||||
buttons = element.all(by.css('button'));
|
||||
});
|
||||
|
||||
it('should populate the UI with initial values', () => {
|
||||
expect(inputs.get(0).getAttribute('value')).toEqual('SF');
|
||||
expect(inputs.get(1).getAttribute('value')).toEqual('NY');
|
||||
});
|
||||
|
||||
it('should add inputs programmatically', () => {
|
||||
expect(inputs.count()).toBe(2);
|
||||
|
||||
buttons.get(1).click();
|
||||
inputs = element.all(by.css('input'));
|
||||
|
||||
expect(inputs.count()).toBe(3);
|
||||
});
|
||||
|
||||
it('should set the value programmatically', () => {
|
||||
buttons.get(2).click();
|
||||
expect(inputs.get(0).getAttribute('value')).toEqual('LA');
|
||||
expect(inputs.get(1).getAttribute('value')).toEqual('MTV');
|
||||
});
|
||||
|
||||
});
|
20
packages/examples/forms/ts/nestedFormArray/module.ts
Normal file
20
packages/examples/forms/ts/nestedFormArray/module.ts
Normal file
@ -0,0 +1,20 @@
|
||||
/**
|
||||
* @license
|
||||
* Copyright 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 https://angular.io/license
|
||||
*/
|
||||
|
||||
import {NgModule} from '@angular/core';
|
||||
import {ReactiveFormsModule} from '@angular/forms';
|
||||
import {BrowserModule} from '@angular/platform-browser';
|
||||
import {NestedFormArray} from './nested_form_array_example';
|
||||
|
||||
@NgModule({
|
||||
imports: [BrowserModule, ReactiveFormsModule],
|
||||
declarations: [NestedFormArray],
|
||||
bootstrap: [NestedFormArray]
|
||||
})
|
||||
export class AppModule {
|
||||
}
|
@ -0,0 +1,49 @@
|
||||
/**
|
||||
* @license
|
||||
* Copyright 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 https://angular.io/license
|
||||
*/
|
||||
|
||||
/* tslint:disable:no-console */
|
||||
// #docregion Component
|
||||
import {Component} from '@angular/core';
|
||||
import {FormArray, FormControl, FormGroup} from '@angular/forms';
|
||||
|
||||
@Component({
|
||||
selector: 'example-app',
|
||||
template: `
|
||||
<form [formGroup]="form" (ngSubmit)="onSubmit()">
|
||||
<div formArrayName="cities">
|
||||
<div *ngFor="let city of cities.controls; let i=index">
|
||||
<input [formControlName]="i" placeholder="City">
|
||||
</div>
|
||||
</div>
|
||||
<button>Submit</button>
|
||||
</form>
|
||||
|
||||
<button (click)="addCity()">Add City</button>
|
||||
<button (click)="setPreset()">Set preset</button>
|
||||
`,
|
||||
})
|
||||
export class NestedFormArray {
|
||||
form = new FormGroup({
|
||||
cities: new FormArray([
|
||||
new FormControl('SF'),
|
||||
new FormControl('NY'),
|
||||
]),
|
||||
});
|
||||
|
||||
get cities(): FormArray { return this.form.get('cities') as FormArray; }
|
||||
|
||||
addCity() { this.cities.push(new FormControl()); }
|
||||
|
||||
onSubmit() {
|
||||
console.log(this.cities.value); // ['SF', 'NY']
|
||||
console.log(this.form.value); // { cities: ['SF', 'NY'] }
|
||||
}
|
||||
|
||||
setPreset() { this.cities.patchValue(['LA', 'MTV']); }
|
||||
}
|
||||
// #enddocregion
|
Reference in New Issue
Block a user