2017-03-15 106 views
2

我有這樣的形式:如何從ngModel獲取價值?

<form id="login-form" class="text-left" (ngSubmit)="submitLogin()"> 
     <div class="login-form-main-message"></div> 
       <div class="main-login-form"> 
        <div class="login-group"> 
         <div class="form-group"> 
          <label for="lg_username" class="sr-only">Username</label> 
          <input type="text" class="form-control" id="lg_username" name="lg_username" placeholder="username" [(ngModel)]="username"> 
         </div> 
         <div class="form-group"> 
          <label for="lg_password" class="sr-only">Password</label> 
          <input type="password" class="form-control" id="lg_password" name="lg_password" placeholder="password" [(ngModel)]="password"> 
         </div> 
        </div> 
        <button type="submit" class="login-button"><i class="fa fa-chevron-right" ></i>LOGIN</button> 
    </div> 
</form> 

在組件我有這樣的:

submitLogin(){ 
      // Variable to hold a reference of addComment/updateComment 

     } 

我已經登錄組件:

import {Component, EventEmitter, Input, OnChanges} from '@angular/core'; 
import {Observable} from 'rxjs/Rx'; 
import { LoginService } from '../services/login.service'; 
import { Login } from '../model/login' 
@Component({ 
    selector: 'login', 
    templateUrl: 'login-form', 

}) 
export class LoginComponent { 
    // Constructor with injected service 
     constructor(
      private commentService: LoginService 
     ){} 
     // Local properties 
     private model = new Login('', ''); 

     submitLogin(){ 
      // Variable to hold a reference of addComment/updateComment 
      alert('aaaa'); 
     } 


} 

現在我想的是,從獲得submitLogin()值ngModel,然後處理這個值,我想要什麼。任何建議?

回答

2

你可以從表單中值:

<form #form="ngForm" id="login-form" class="text-left" (ngSubmit)="submitLogin(form.value)"> 
+0

以及如何訪問組件中的這些值?因爲當我現在使用:this.username我得到一個錯誤 – None

+0

'submitLogin(values){console.log(values); }' –

+1

tnx ..多數民衆贊成它:) – None