docs(forms): update example for formGroupDirective

This commit is contained in:
Kara Erickson 2016-09-11 12:43:43 -07:00 committed by Evan Martin
parent cdda4082de
commit dd8204a655
5 changed files with 59 additions and 77 deletions

View File

@ -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');
});
});
});

View File

@ -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 {
}

View File

@ -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'}); }
}

View File

@ -28,7 +28,8 @@ export const controlNameBinding: any = {
};
/**
* @whatItDoes Syncs a form control element to an existing {@link FormControl} instance by name.
* @whatItDoes Syncs a {@link FormControl} in an existing {@link FormGroup} to a form control
* element by name.
*
* In other words, this directive ensures that any values written to the {@link FormControl}
* instance programmatically will be written to the DOM element (model -> view). Conversely,
@ -37,24 +38,34 @@ export const controlNameBinding: any = {
*
* @howToUse
*
* This directive must be used with a parent {@link FormGroupDirective} (selector: `[formGroup]`).
* This directive is designed to be used with a parent {@link FormGroupDirective} (selector:
* `[formGroup]`).
*
* Pass in the string name of the {@link FormControl} instance you want to link. It will look for a
* control registered with that name in the {@link FormGroup} passed to the parent
* {@link FormGroupDirective}.
* It accepts the string name of the {@link FormControl} instance you want to
* link, and will look for a {@link FormControl} registered with that name in the
* closest {@link FormGroup} or {@link FormArray} above it.
*
* You can initialize the value of the form when instantiating the {@link FormGroup}, or you can
* set it programmatically later using {@link AbstractControl.setValue} or
* {@link AbstractControl.patchValue}.
* **Access the control**: You can access the {@link FormControl} associated with
* this directive by using the {@link AbstractControl.get} method.
* Ex: `this.form.get('first');`
*
* If you want to listen to changes in the value of the form, you can subscribe to the
* {@link AbstractControl.valueChanges} event.
* **Get value**: the `value` property is always synced and available on the {@link FormControl}.
* See a full list of available properties in {@link AbstractControl}.
*
* **Set value**: You can set an initial value for the control when instantiating the
* {@link FormControl}, or you can set it programmatically later using
* {@link AbstractControl.setValue} or {@link AbstractControl.patchValue}.
*
* **Listen to value**: If you want to listen to changes in the value of the control, you can
* subscribe to the {@link AbstractControl.valueChanges} event. You can also listen to
* {@link AbstractControl.statusChanges} to be notified when the validation status is
* re-calculated.
*
* ### Example
*
* In this example, we create form controls for first name and last name.
*
* {@example forms/ts/formControlName/form_control_name_example.ts region='Component'}
* {@example forms/ts/simpleFormGroup/simple_form_group_example.ts region='Component'}
*
* * **npm package**: `@angular/forms`
*

View File

@ -27,72 +27,33 @@ export const formDirectiveProvider: any = {
};
/**
* Binds an existing form group to a DOM element. It requires importing the {@link
* ReactiveFormsModule}.
* @whatItDoes Binds an existing {@link FormGroup} to a DOM element.
*
* In this example, we bind the form group to the form element, and we bind the login and
* password controls to the login and password elements.
* @howToUse
*
* ```typescript
* @Component({
* selector: 'my-app',
* template: `
* <div>
* <h2>Binding an existing form group</h2>
* <form [formGroup]="loginForm">
* <p>Login: <input type="text" formControlName="login"></p>
* <p>Password: <input type="password" formControlName="password"></p>
* </form>
* <p>Value:</p>
* <pre>{{ loginForm.value | json}}</pre>
* </div>
* `
* })
* export class App {
* loginForm: FormGroup;
* This directive accepts an existing {@link FormGroup} instance. It will then use this
* {@link FormGroup} instance to match any child {@link FormControl}, {@link FormGroup},
* and {@link FormArray} instances to child {@link FormControlName}, {@link FormGroupName},
* and {@link FormArrayName} directives.
*
* constructor() {
* this.loginForm = new FormGroup({
* login: new FormControl(""),
* password: new FormControl("")
* });
* }
* **Set value**: You can set the form's initial value when instantiating the
* {@link FormGroup}, or you can set it programmatically later using the {@link FormGroup}'s
* {@link AbstractControl.setValue} or {@link AbstractControl.patchValue} methods.
*
* }
* ```
* **Listen to value**: If you want to listen to changes in the value of the form, you can subscribe
* to the {@link FormGroup}'s {@link AbstractControl.valueChanges} event. You can also listen to
* its {@link AbstractControl.statusChanges} event to be notified when the validation status is
* re-calculated.
*
* We can also use setValue() to populate the form programmatically.
* ### Example
*
* ```typescript
* @Component({
* selector: "login-comp",
* template: `
* <form [formGroup]='loginForm'>
* Login <input type='text' formControlName='login'>
* Password <input type='password' formControlName='password'>
* <button (click)="onLogin()">Login</button>
* </form>`
* })
* class LoginComp {
* loginForm: FormGroup;
* In this example, we create form controls for first name and last name.
*
* constructor() {
* this.loginForm = new FormGroup({
* login: new FormControl(''),
* password: new FormControl('')
* });
* }
* {@example forms/ts/simpleFormGroup/simple_form_group_example.ts region='Component'}
*
* populate() {
* this.loginForm.setValue({ login: 'some login', password: 'some password'});
* }
* **npm package**: `@angular/forms`
*
* onLogin(): void {
* // this.credentials.login === 'some login'
* // this.credentials.password === 'some password'
* }
* }
* ```
* **NgModule**: {@link ReactiveFormsModule}
*
* @stable
*/