docs(forms): add example apps for ngModelGroup (#11525)
This commit is contained in:
@ -0,0 +1,41 @@
|
||||
/**
|
||||
* @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 {verifyNoBrowserErrors} from '../../../../_common/e2e_util';
|
||||
|
||||
describe('ngModelGroup example', () => {
|
||||
afterEach(verifyNoBrowserErrors);
|
||||
let inputs: ElementFinder;
|
||||
let buttons: ElementFinder;
|
||||
|
||||
beforeEach(() => {
|
||||
browser.get('/forms/ts/ngModelGroup/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('Nancy');
|
||||
expect(inputs.get(1).getAttribute('value')).toEqual('Drew');
|
||||
});
|
||||
|
||||
it('should show the error when name is invalid', () => {
|
||||
inputs.get(0).click();
|
||||
inputs.get(0).clear();
|
||||
inputs.get(0).sendKeys('a');
|
||||
|
||||
expect(element(by.css('p')).getText()).toEqual('Name is invalid.');
|
||||
});
|
||||
|
||||
it('should set the value when changing the domain model', () => {
|
||||
buttons.get(1).click();
|
||||
expect(inputs.get(0).getAttribute('value')).toEqual('Bess');
|
||||
expect(inputs.get(1).getAttribute('value')).toEqual('Marvin');
|
||||
});
|
||||
|
||||
});
|
20
modules/@angular/examples/forms/ts/ngModelGroup/module.ts
Normal file
20
modules/@angular/examples/forms/ts/ngModelGroup/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 {FormsModule} from '@angular/forms';
|
||||
import {BrowserModule} from '@angular/platform-browser';
|
||||
import {NgModelGroupComp} from './ng_model_group_example';
|
||||
|
||||
@NgModule({
|
||||
imports: [BrowserModule, FormsModule],
|
||||
declarations: [NgModelGroupComp],
|
||||
bootstrap: [NgModelGroupComp]
|
||||
})
|
||||
export class AppModule {
|
||||
}
|
@ -0,0 +1,41 @@
|
||||
/**
|
||||
* @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
|
||||
*/
|
||||
|
||||
// #docregion Component
|
||||
import {Component} from '@angular/core';
|
||||
import {NgForm} from '@angular/forms';
|
||||
|
||||
@Component({
|
||||
selector: 'example-app',
|
||||
template: `
|
||||
<form #f="ngForm" (ngSubmit)="onSubmit(f)">
|
||||
<p *ngIf="nameCtrl.invalid">Name is invalid.</p>
|
||||
|
||||
<div ngModelGroup="name" #nameCtrl="ngModelGroup">
|
||||
<input name="first" [ngModel]="name.first" minlength="2">
|
||||
<input name="last" [ngModel]="name.last" required>
|
||||
</div>
|
||||
|
||||
<input name="email" ngModel>
|
||||
<button>Submit</button>
|
||||
</form>
|
||||
|
||||
<button (click)="setValue()">Set value</button>
|
||||
`,
|
||||
})
|
||||
export class NgModelGroupComp {
|
||||
name = {first: 'Nancy', last: 'Drew'};
|
||||
|
||||
onSubmit(f: NgForm) {
|
||||
console.log(f.value); // {name: {first: 'Nancy', last: 'Drew'}, email: ''}
|
||||
console.log(f.valid); // true
|
||||
}
|
||||
|
||||
setValue() { this.name = {first: 'Bess', last: 'Marvin'}; }
|
||||
}
|
||||
// #enddocregion
|
Reference in New Issue
Block a user