310 lines
		
	
	
		
			4.2 KiB
		
	
	
	
		
			Plaintext
		
	
	
	
	
	
			
		
		
	
	
			310 lines
		
	
	
		
			4.2 KiB
		
	
	
	
		
			Plaintext
		
	
	
	
	
	
 | 
						|
p.location-badge.
 | 
						|
  exported from <a href='../forms'>angular2/forms</a>
 | 
						|
  defined in <a href="https://github.com/angular/angular/tree/2.0.0-alpha.34/modules/angular2/src/forms/directives/ng_form_model.ts#L15-L142">angular2/src/forms/directives/ng_form_model.ts (line 15)</a>
 | 
						|
 | 
						|
:markdown
 | 
						|
  Binds an existing control group to a DOM element.
 | 
						|
  
 | 
						|
  # Example
 | 
						|
  
 | 
						|
  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"})
 | 
						|
  @View({
 | 
						|
       directives: [formDirectives],
 | 
						|
       template: "<form [ng-form-model]='loginForm'>" +
 | 
						|
               "Login <input type='text' ng-control='login'>" +
 | 
						|
               "Password <input type='password' ng-control='password'>" +
 | 
						|
               "<button (click)="onLogin()">Login</button>" +
 | 
						|
               "</form>"
 | 
						|
       })
 | 
						|
  class LoginComp {
 | 
						|
   loginForm:ControlGroup;
 | 
						|
  
 | 
						|
   constructor() {
 | 
						|
     this.loginForm = new ControlGroup({
 | 
						|
       login: new Control(""),
 | 
						|
       password: new Control("")
 | 
						|
     });
 | 
						|
   }
 | 
						|
  
 | 
						|
   onLogin() {
 | 
						|
     // this.loginForm.value
 | 
						|
   }
 | 
						|
  }
 | 
						|
  
 | 
						|
   ```
 | 
						|
  
 | 
						|
  We can also use ng-model to bind a domain model to the form.
 | 
						|
  
 | 
						|
   ```
 | 
						|
  @Component({selector: "login-comp"})
 | 
						|
  @View({
 | 
						|
       directives: [formDirectives],
 | 
						|
       template: "<form [ng-form-model]='loginForm'>" +
 | 
						|
               "Login <input type='text' ng-control='login' [(ng-model)]='login'>" +
 | 
						|
               "Password <input type='password' ng-control='password' [(ng-model)]='password'>" +
 | 
						|
               "<button (click)="onLogin()">Login</button>" +
 | 
						|
               "</form>"
 | 
						|
       })
 | 
						|
  class LoginComp {
 | 
						|
   credentials:{login:string, password:string}
 | 
						|
   loginForm:ControlGroup;
 | 
						|
  
 | 
						|
   constructor() {
 | 
						|
     this.loginForm = new ControlGroup({
 | 
						|
       login: new Control(""),
 | 
						|
       password: new Control("")
 | 
						|
     });
 | 
						|
   }
 | 
						|
  
 | 
						|
   onLogin() {
 | 
						|
     // this.credentials.login === 'some login'
 | 
						|
     // this.credentials.password === 'some password'
 | 
						|
   }
 | 
						|
  }
 | 
						|
   ```
 | 
						|
  
 | 
						|
  
 | 
						|
 | 
						|
.l-main-section
 | 
						|
  h2 Annotations
 | 
						|
  .l-sub-section
 | 
						|
    h3.annotation Directive
 | 
						|
    pre.prettyprint
 | 
						|
      code.
 | 
						|
        @Directive({
 | 
						|
          selector: '[ng-form-model]',
 | 
						|
          bindings: [formDirectiveBinding],
 | 
						|
          properties: ['form: ng-form-model'],
 | 
						|
          lifecycle: [LifecycleEvent.onChange],
 | 
						|
          host: {
 | 
						|
            '(submit)': 'onSubmit()',
 | 
						|
          },
 | 
						|
          events: ['ngSubmit'],
 | 
						|
          exportAs: 'form'
 | 
						|
        })
 | 
						|
 | 
						|
 | 
						|
.l-main-section
 | 
						|
  h2 Members
 | 
						|
  .l-sub-section
 | 
						|
    h3 form
 | 
						|
 | 
						|
    
 | 
						|
    :markdown
 | 
						|
      
 | 
						|
      
 | 
						|
 | 
						|
 | 
						|
 | 
						|
 | 
						|
 | 
						|
  .l-sub-section
 | 
						|
    h3 directives
 | 
						|
 | 
						|
    
 | 
						|
    :markdown
 | 
						|
      
 | 
						|
      
 | 
						|
 | 
						|
 | 
						|
 | 
						|
 | 
						|
 | 
						|
  .l-sub-section
 | 
						|
    h3 ngSubmit
 | 
						|
 | 
						|
    
 | 
						|
    :markdown
 | 
						|
      
 | 
						|
      
 | 
						|
 | 
						|
 | 
						|
 | 
						|
 | 
						|
 | 
						|
  .l-sub-section
 | 
						|
    h3 onChange
 | 
						|
 | 
						|
    
 | 
						|
    pre.prettyprint
 | 
						|
      code.
 | 
						|
        onChange(_: any)
 | 
						|
    
 | 
						|
    :markdown
 | 
						|
      
 | 
						|
      
 | 
						|
 | 
						|
 | 
						|
 | 
						|
 | 
						|
 | 
						|
  .l-sub-section
 | 
						|
    h3 formDirective
 | 
						|
 | 
						|
    
 | 
						|
    :markdown
 | 
						|
      
 | 
						|
      
 | 
						|
 | 
						|
 | 
						|
 | 
						|
 | 
						|
 | 
						|
  .l-sub-section
 | 
						|
    h3 control
 | 
						|
 | 
						|
    
 | 
						|
    :markdown
 | 
						|
      
 | 
						|
      
 | 
						|
 | 
						|
 | 
						|
 | 
						|
 | 
						|
 | 
						|
  .l-sub-section
 | 
						|
    h3 path
 | 
						|
 | 
						|
    
 | 
						|
    :markdown
 | 
						|
      
 | 
						|
      
 | 
						|
 | 
						|
 | 
						|
 | 
						|
 | 
						|
 | 
						|
  .l-sub-section
 | 
						|
    h3 addControl
 | 
						|
 | 
						|
    
 | 
						|
    pre.prettyprint
 | 
						|
      code.
 | 
						|
        addControl(dir: NgControl)
 | 
						|
    
 | 
						|
    :markdown
 | 
						|
      
 | 
						|
      
 | 
						|
 | 
						|
 | 
						|
 | 
						|
 | 
						|
 | 
						|
  .l-sub-section
 | 
						|
    h3 getControl
 | 
						|
 | 
						|
    
 | 
						|
    pre.prettyprint
 | 
						|
      code.
 | 
						|
        getControl(dir: NgControl)
 | 
						|
    
 | 
						|
    :markdown
 | 
						|
      
 | 
						|
      
 | 
						|
 | 
						|
 | 
						|
 | 
						|
 | 
						|
 | 
						|
  .l-sub-section
 | 
						|
    h3 removeControl
 | 
						|
 | 
						|
    
 | 
						|
    pre.prettyprint
 | 
						|
      code.
 | 
						|
        removeControl(dir: NgControl)
 | 
						|
    
 | 
						|
    :markdown
 | 
						|
      
 | 
						|
      
 | 
						|
 | 
						|
 | 
						|
 | 
						|
 | 
						|
 | 
						|
  .l-sub-section
 | 
						|
    h3 addControlGroup
 | 
						|
 | 
						|
    
 | 
						|
    pre.prettyprint
 | 
						|
      code.
 | 
						|
        addControlGroup(dir: NgControlGroup)
 | 
						|
    
 | 
						|
    :markdown
 | 
						|
      
 | 
						|
      
 | 
						|
 | 
						|
 | 
						|
 | 
						|
 | 
						|
 | 
						|
  .l-sub-section
 | 
						|
    h3 removeControlGroup
 | 
						|
 | 
						|
    
 | 
						|
    pre.prettyprint
 | 
						|
      code.
 | 
						|
        removeControlGroup(dir: NgControlGroup)
 | 
						|
    
 | 
						|
    :markdown
 | 
						|
      
 | 
						|
      
 | 
						|
 | 
						|
 | 
						|
 | 
						|
 | 
						|
 | 
						|
  .l-sub-section
 | 
						|
    h3 getControlGroup
 | 
						|
 | 
						|
    
 | 
						|
    pre.prettyprint
 | 
						|
      code.
 | 
						|
        getControlGroup(dir: NgControlGroup)
 | 
						|
    
 | 
						|
    :markdown
 | 
						|
      
 | 
						|
      
 | 
						|
 | 
						|
 | 
						|
 | 
						|
 | 
						|
 | 
						|
  .l-sub-section
 | 
						|
    h3 updateModel
 | 
						|
 | 
						|
    
 | 
						|
    pre.prettyprint
 | 
						|
      code.
 | 
						|
        updateModel(dir: NgControl, value: any)
 | 
						|
    
 | 
						|
    :markdown
 | 
						|
      
 | 
						|
      
 | 
						|
 | 
						|
 | 
						|
 | 
						|
 | 
						|
 | 
						|
  .l-sub-section
 | 
						|
    h3 onSubmit
 | 
						|
 | 
						|
    
 | 
						|
    pre.prettyprint
 | 
						|
      code.
 | 
						|
        onSubmit()
 | 
						|
    
 | 
						|
    :markdown
 | 
						|
      
 | 
						|
      
 | 
						|
 | 
						|
 | 
						|
 | 
						|
 |