From 711ab6d573ebe0d26a426ea9202b705017cc9c0d Mon Sep 17 00:00:00 2001 From: Alex Rickabaugh Date: Wed, 23 Sep 2015 13:28:50 -0700 Subject: [PATCH] docs(forms): Add documentation and live examples for NgForm, NgFormControl, NgFormModel, and NgModel. Closes #4343 --- .../src/core/forms/directives/ng_form.ts | 78 +++++++++++++------ .../core/forms/directives/ng_form_control.ts | 36 ++++++--- .../core/forms/directives/ng_form_model.ts | 54 +++++++------ .../src/core/forms/directives/ng_model.ts | 12 ++- 4 files changed, 117 insertions(+), 63 deletions(-) diff --git a/modules/angular2/src/core/forms/directives/ng_form.ts b/modules/angular2/src/core/forms/directives/ng_form.ts index b31bf7d49e..4d5a38743a 100644 --- a/modules/angular2/src/core/forms/directives/ng_form.ts +++ b/modules/angular2/src/core/forms/directives/ng_form.ts @@ -19,36 +19,64 @@ const formDirectiveBinding = CONST_EXPR(new Binding(ControlContainer, {toAlias: forwardRef(() => NgForm)})); /** - * Creates and binds a form object to a DOM element. + * If `NgForm` is bound in a component, `
` elements in that component will be + * upgraded to use the Angular form system. * - * # Example + * # Typical Use * - * ``` - * @Component({selector: "signup-comp"}) + * Include `FORM_DIRECTIVES` in the `directives` section of a {@link View} annotation + * to use `NgForm` and its associated controls. + * + * # Structure + * + * An Angular form is a collection of {@link Control}s in some hierarchy. + * `Control`s can be at the top level or can be organized in {@link ControlGroups} + * or {@link ControlArray}s. This hierarchy is reflected in the form's `value`, a + * JSON object that mirrors the form structure. + * + * # Submission + * + * The `ng-submit` event signals when the user triggers a form submission. + * + * ### Example ([live demo](http://plnkr.co/edit/ltdgYj4P0iY64AR71EpL?p=preview)) + * + * ```typescript + * @Component({ + * selector: 'my-app' + * }) * @View({ - * directives: [FORM_DIRECTIVES], - * template: ` - * - *
- * Login - * Password - *
- *
Credentials are invalid
+ * template: ` + *
+ *

Submit the form to see the data object Angular builds

+ *

NgForm demo

+ * + *

Control group: credentials

+ *
+ *

Login:

+ *

Password:

+ *
+ *

Control group: person

+ *
+ *

First name:

+ *

Last name:

+ *
+ * + *

Form data submitted:

+ * + *
{{data}}
+ *
+ * `, + * directives: [CORE_DIRECTIVES, FORM_DIRECTIVES] + * }) + * export class App { + * constructor() {} * - *
- * Name - *
- * - * - * `}) - * class SignupComp { - * onSignUp(value): void { - * // value === { - * // personal: {name: 'some name'}, - * // credentials: {login: 'some login', password: 'some password'}} - * } + * data: string; + * + * onSubmit(data) { + * this.data = JSON.stringify(data, null, 2); + * } * } - * * ``` */ @Directive({ diff --git a/modules/angular2/src/core/forms/directives/ng_form_control.ts b/modules/angular2/src/core/forms/directives/ng_form_control.ts index ee67f7b128..7745998ad2 100644 --- a/modules/angular2/src/core/forms/directives/ng_form_control.ts +++ b/modules/angular2/src/core/forms/directives/ng_form_control.ts @@ -12,29 +12,43 @@ const formControlBinding = CONST_EXPR(new Binding(NgControl, {toAlias: forwardRef(() => NgFormControl)})); /** - * Binds an existing control to a DOM element. + * Binds an existing {@link Control} to a DOM element. * - * # Example + * ### Example ([live demo](http://plnkr.co/edit/jcQlZ2tTh22BZZ2ucNAT?p=preview)) * * In this example, we bind the control to an input element. When the value of the input element * changes, the value of the control will reflect that change. Likewise, if the value of the * control changes, the input element reflects that change. * - * ``` - * @Component({selector: "login-comp"}) + * ```typescript + * @Component({ + * selector: 'my-app' + * }) * @View({ - * directives: [FORM_DIRECTIVES], - * template: "" - * }) - * class LoginComp { - * loginControl: Control = new Control('');; + * template: ` + *
+ *

NgFormControl Example

+ *
+ *

Element with existing control:

+ *

Value of existing control: {{loginControl.value}}

+ *
+ *
+ * `, + * directives: [CORE_DIRECTIVES, FORM_DIRECTIVES] + * }) + * export class App { + * loginControl: Control = new Control(''); * } - * * ``` * + * # ng-model + * * We can also use `ng-model` to bind a domain model to the form. * - * ``` + * ### Example ([live demo](http://plnkr.co/edit/yHMLuHO7DNgT8XvtjTDH?p=preview)) + * + * ```typescript * @Component({selector: "login-comp"}) * @View({ * directives: [FORM_DIRECTIVES], diff --git a/modules/angular2/src/core/forms/directives/ng_form_model.ts b/modules/angular2/src/core/forms/directives/ng_form_model.ts index 78ce068a1c..b00fb5c176 100644 --- a/modules/angular2/src/core/forms/directives/ng_form_model.ts +++ b/modules/angular2/src/core/forms/directives/ng_form_model.ts @@ -18,42 +18,48 @@ const formDirectiveBinding = /** * Binds an existing control group to a DOM element. * - * # Example + * ### Example ([live demo](http://plnkr.co/edit/jqrVirudY8anJxTMUjTP?p=preview)) * * In this example, we bind the control group to the form element, and we bind the login and * password controls to the login and password elements. * - * ``` - * @Component({selector: "login-comp"}) + * ```typescript + * @Component({ + * selector: 'my-app' + * }) * @View({ - * directives: [FORM_DIRECTIVES], - * template: ` - *
- * Login - * Password - * - *
` - * }) - * class LoginComp { - * loginForm: ControlGroup; + * template: ` + *
+ *

NgFormModel Example

+ *
+ *

Login:

+ *

Password:

+ *
+ *

Value:

+ *
{{value}}
+ *
+ * `, + * directives: [FORM_DIRECTIVES] + * }) + * export class App { + * loginForm: ControlGroup; * - * constructor() { - * this.loginForm = new ControlGroup({ - * login: new Control(""), - * password: new Control("") - * }); - * } + * constructor() { + * this.loginForm = new ControlGroup({ + * login: new Control(""), + * password: new Control("") + * }); + * } * - * onLogin(): void { - * // this.loginForm.value - * } + * get value(): string { + * return JSON.stringify(this.loginForm.value, null, 2); + * } * } - * * ``` * * We can also use ng-model to bind a domain model to the form. * - * ``` + * ```typescript * @Component({selector: "login-comp"}) * @View({ * directives: [FORM_DIRECTIVES], diff --git a/modules/angular2/src/core/forms/directives/ng_model.ts b/modules/angular2/src/core/forms/directives/ng_model.ts index 3e125798e2..9d985a4ab5 100644 --- a/modules/angular2/src/core/forms/directives/ng_model.ts +++ b/modules/angular2/src/core/forms/directives/ng_model.ts @@ -13,10 +13,16 @@ import {setUpControl, isPropertyUpdated} from './shared'; const formControlBinding = CONST_EXPR(new Binding(NgControl, {toAlias: forwardRef(() => NgModel)})); /** - * Binds a domain model to the form. + * Binds a domain model to a form control. * - * # Example - * ``` + * # Usage + * + * `ng-model` binds an existing domain model to a form control. For a + * two-way binding, use `[(ng-model)]` to ensure the model updates in + * both directions. + * + * ### Example ([live demo](http://plnkr.co/edit/R3UX5qDaUqFO2VYR0UzH?p=preview)) + * ```typescript * @Component({selector: "search-comp"}) * @View({ * directives: [FORM_DIRECTIVES],