Skip to content

Commit 711ab6d

Browse files
committed
docs(forms): Add documentation and live examples for NgForm, NgFormControl, NgFormModel, and NgModel.
Closes angular#4343
1 parent 4e5c663 commit 711ab6d

File tree

4 files changed

+117
-63
lines changed

4 files changed

+117
-63
lines changed

modules/angular2/src/core/forms/directives/ng_form.ts

Lines changed: 53 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -19,36 +19,64 @@ const formDirectiveBinding =
1919
CONST_EXPR(new Binding(ControlContainer, {toAlias: forwardRef(() => NgForm)}));
2020

2121
/**
22-
* Creates and binds a form object to a DOM element.
22+
* If `NgForm` is bound in a component, `<form>` elements in that component will be
23+
* upgraded to use the Angular form system.
2324
*
24-
* # Example
25+
* # Typical Use
2526
*
26-
* ```
27-
* @Component({selector: "signup-comp"})
27+
* Include `FORM_DIRECTIVES` in the `directives` section of a {@link View} annotation
28+
* to use `NgForm` and its associated controls.
29+
*
30+
* # Structure
31+
*
32+
* An Angular form is a collection of {@link Control}s in some hierarchy.
33+
* `Control`s can be at the top level or can be organized in {@link ControlGroups}
34+
* or {@link ControlArray}s. This hierarchy is reflected in the form's `value`, a
35+
* JSON object that mirrors the form structure.
36+
*
37+
* # Submission
38+
*
39+
* The `ng-submit` event signals when the user triggers a form submission.
40+
*
41+
* ### Example ([live demo](http://plnkr.co/edit/ltdgYj4P0iY64AR71EpL?p=preview))
42+
*
43+
* ```typescript
44+
* @Component({
45+
* selector: 'my-app'
46+
* })
2847
* @View({
29-
* directives: [FORM_DIRECTIVES],
30-
* template: `
31-
* <form #f="form" (submit)='onSignUp(f.value)'>
32-
* <div ng-control-group='credentials' #credentials="form">
33-
* Login <input type='text' ng-control='login'>
34-
* Password <input type='password' ng-control='password'>
35-
* </div>
36-
* <div *ng-if="!credentials.valid">Credentials are invalid</div>
48+
* template: `
49+
* <div>
50+
* <p>Submit the form to see the data object Angular builds</p>
51+
* <h2>NgForm demo</h2>
52+
* <form #f="form" (ng-submit)="onSubmit(f.value)">
53+
* <h3>Control group: credentials</h3>
54+
* <div ng-control-group="credentials">
55+
* <p>Login: <input type="text" ng-control="login"></p>
56+
* <p>Password: <input type="password" ng-control="password"></p>
57+
* </div>
58+
* <h3>Control group: person</h3>
59+
* <div ng-control-group="person">
60+
* <p>First name: <input type="text" ng-control="firstName"></p>
61+
* <p>Last name: <input type="text" ng-control="lastName"></p>
62+
* </div>
63+
* <button type="submit">Submit Form</button>
64+
* <p>Form data submitted:</p>
65+
* </form>
66+
* <pre>{{data}}</pre>
67+
* </div>
68+
* `,
69+
* directives: [CORE_DIRECTIVES, FORM_DIRECTIVES]
70+
* })
71+
* export class App {
72+
* constructor() {}
3773
*
38-
* <div ng-control-group='personal'>
39-
* Name <input type='text' ng-control='name'>
40-
* </div>
41-
* <button type='submit'>Sign Up!</button>
42-
* </form>
43-
* `})
44-
* class SignupComp {
45-
* onSignUp(value): void {
46-
* // value === {
47-
* // personal: {name: 'some name'},
48-
* // credentials: {login: 'some login', password: 'some password'}}
49-
* }
50-
* }
74+
* data: string;
5175
*
76+
* onSubmit(data) {
77+
* this.data = JSON.stringify(data, null, 2);
78+
* }
79+
* }
5280
* ```
5381
*/
5482
@Directive({

modules/angular2/src/core/forms/directives/ng_form_control.ts

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,29 +12,43 @@ const formControlBinding =
1212
CONST_EXPR(new Binding(NgControl, {toAlias: forwardRef(() => NgFormControl)}));
1313

1414
/**
15-
* Binds an existing control to a DOM element.
15+
* Binds an existing {@link Control} to a DOM element.
1616
*
17-
* # Example
17+
* ### Example ([live demo](http://plnkr.co/edit/jcQlZ2tTh22BZZ2ucNAT?p=preview))
1818
*
1919
* In this example, we bind the control to an input element. When the value of the input element
2020
* changes, the value of the control will reflect that change. Likewise, if the value of the
2121
* control changes, the input element reflects that change.
2222
*
23-
* ```
24-
* @Component({selector: "login-comp"})
23+
* ```typescript
24+
* @Component({
25+
* selector: 'my-app'
26+
* })
2527
* @View({
26-
* directives: [FORM_DIRECTIVES],
27-
* template: "<input type='text' [ng-form-control]='loginControl'>"
28-
* })
29-
* class LoginComp {
30-
* loginControl: Control = new Control('');;
28+
* template: `
29+
* <div>
30+
* <h2>NgFormControl Example</h2>
31+
* <form>
32+
* <p>Element with existing control: <input type="text"
33+
* [ng-form-control]="loginControl"></p>
34+
* <p>Value of existing control: {{loginControl.value}}</p>
35+
* </form>
36+
* </div>
37+
* `,
38+
* directives: [CORE_DIRECTIVES, FORM_DIRECTIVES]
39+
* })
40+
* export class App {
41+
* loginControl: Control = new Control('');
3142
* }
32-
*
3343
* ```
3444
*
45+
* # ng-model
46+
*
3547
* We can also use `ng-model` to bind a domain model to the form.
3648
*
37-
* ```
49+
* ### Example ([live demo](http://plnkr.co/edit/yHMLuHO7DNgT8XvtjTDH?p=preview))
50+
*
51+
* ```typescript
3852
* @Component({selector: "login-comp"})
3953
* @View({
4054
* directives: [FORM_DIRECTIVES],

modules/angular2/src/core/forms/directives/ng_form_model.ts

Lines changed: 30 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -18,42 +18,48 @@ const formDirectiveBinding =
1818
/**
1919
* Binds an existing control group to a DOM element.
2020
*
21-
* # Example
21+
* ### Example ([live demo](http://plnkr.co/edit/jqrVirudY8anJxTMUjTP?p=preview))
2222
*
2323
* In this example, we bind the control group to the form element, and we bind the login and
2424
* password controls to the login and password elements.
2525
*
26-
* ```
27-
* @Component({selector: "login-comp"})
26+
* ```typescript
27+
* @Component({
28+
* selector: 'my-app'
29+
* })
2830
* @View({
29-
* directives: [FORM_DIRECTIVES],
30-
* template: `
31-
* <form [ng-form-model]='loginForm'>
32-
* Login <input type='text' ng-control='login'>
33-
* Password <input type='password' ng-control='password'>
34-
* <button (click)="onLogin()">Login</button>
35-
* </form>`
36-
* })
37-
* class LoginComp {
38-
* loginForm: ControlGroup;
31+
* template: `
32+
* <div>
33+
* <h2>NgFormModel Example</h2>
34+
* <form [ng-form-model]="loginForm">
35+
* <p>Login: <input type="text" ng-control="login"></p>
36+
* <p>Password: <input type="password" ng-control="password"></p>
37+
* </form>
38+
* <p>Value:</p>
39+
* <pre>{{value}}</pre>
40+
* </div>
41+
* `,
42+
* directives: [FORM_DIRECTIVES]
43+
* })
44+
* export class App {
45+
* loginForm: ControlGroup;
3946
*
40-
* constructor() {
41-
* this.loginForm = new ControlGroup({
42-
* login: new Control(""),
43-
* password: new Control("")
44-
* });
45-
* }
47+
* constructor() {
48+
* this.loginForm = new ControlGroup({
49+
* login: new Control(""),
50+
* password: new Control("")
51+
* });
52+
* }
4653
*
47-
* onLogin(): void {
48-
* // this.loginForm.value
49-
* }
54+
* get value(): string {
55+
* return JSON.stringify(this.loginForm.value, null, 2);
56+
* }
5057
* }
51-
*
5258
* ```
5359
*
5460
* We can also use ng-model to bind a domain model to the form.
5561
*
56-
* ```
62+
* ```typescript
5763
* @Component({selector: "login-comp"})
5864
* @View({
5965
* directives: [FORM_DIRECTIVES],

modules/angular2/src/core/forms/directives/ng_model.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,16 @@ import {setUpControl, isPropertyUpdated} from './shared';
1313
const formControlBinding = CONST_EXPR(new Binding(NgControl, {toAlias: forwardRef(() => NgModel)}));
1414

1515
/**
16-
* Binds a domain model to the form.
16+
* Binds a domain model to a form control.
1717
*
18-
* # Example
19-
* ```
18+
* # Usage
19+
*
20+
* `ng-model` binds an existing domain model to a form control. For a
21+
* two-way binding, use `[(ng-model)]` to ensure the model updates in
22+
* both directions.
23+
*
24+
* ### Example ([live demo](http://plnkr.co/edit/R3UX5qDaUqFO2VYR0UzH?p=preview))
25+
* ```typescript
2026
* @Component({selector: "search-comp"})
2127
* @View({
2228
* directives: [FORM_DIRECTIVES],

0 commit comments

Comments
 (0)