docs(forms): update example for formGroupDirective
This commit is contained in:

committed by
Evan Martin

parent
cdda4082de
commit
dd8204a655
@ -16,7 +16,7 @@ describe('formControlName example', () => {
|
||||
let lastInput: ElementFinder;
|
||||
|
||||
beforeEach(() => {
|
||||
browser.get('/forms/ts/formControlName/index.html');
|
||||
browser.get('/forms/ts/simpleFormGroup/index.html');
|
||||
firstInput = element(by.css('[formControlName="first"]'));
|
||||
lastInput = element(by.css('[formControlName="last"]'));
|
||||
});
|
||||
@ -34,5 +34,11 @@ describe('formControlName example', () => {
|
||||
expect(element(by.css('div')).getText()).toEqual('Name is too short.');
|
||||
});
|
||||
|
||||
it('should set the value programmatically', () => {
|
||||
element(by.css('button:not([type="submit"])')).click();
|
||||
expect(firstInput.getAttribute('value')).toEqual('Carson');
|
||||
expect(lastInput.getAttribute('value')).toEqual('Drew');
|
||||
});
|
||||
|
||||
});
|
||||
});
|
@ -9,12 +9,12 @@
|
||||
import {NgModule} from '@angular/core';
|
||||
import {ReactiveFormsModule} from '@angular/forms';
|
||||
import {BrowserModule} from '@angular/platform-browser';
|
||||
import {FormControlNameComp} from './form_control_name_example';
|
||||
import {SimpleFormGroup} from './simple_form_group_example';
|
||||
|
||||
@NgModule({
|
||||
imports: [BrowserModule, ReactiveFormsModule],
|
||||
declarations: [FormControlNameComp],
|
||||
bootstrap: [FormControlNameComp]
|
||||
declarations: [SimpleFormGroup],
|
||||
bootstrap: [SimpleFormGroup]
|
||||
})
|
||||
export class AppModule {
|
||||
}
|
@ -21,18 +21,22 @@ import {FormControl, FormGroup, Validators} from '@angular/forms';
|
||||
|
||||
<button type="submit">Submit</button>
|
||||
</form>
|
||||
|
||||
`
|
||||
<button (click)="setValue()">Set preset value</button>
|
||||
`,
|
||||
})
|
||||
export class FormControlNameComp {
|
||||
form = new FormGroup(
|
||||
{first: new FormControl('Nancy', Validators.minLength(2)), last: new FormControl('Drew')});
|
||||
export class SimpleFormGroup {
|
||||
form = new FormGroup({
|
||||
first: new FormControl('Nancy', Validators.minLength(2)),
|
||||
last: new FormControl('Drew'),
|
||||
});
|
||||
|
||||
get first() { return this.form.get('first'); }
|
||||
|
||||
onSubmit(): void {
|
||||
console.log(this.form.value); // {first: 'Nancy', last: 'Drew'}
|
||||
}
|
||||
|
||||
setValue() { this.form.setValue({first: 'Carson', last: 'Drew'}); }
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user