简体   繁体   English

如何从ngModel获取价值?

[英]How to get value from ngModel?

I have this form: 我有这样的形式:

    <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>

In component i have this: 在组件我有这个:

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

      }

I have login component: 我有登录组件:

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


}

Now what i want is to get value in submitLogin() from ngModel and then handle that values what ever i want. 现在我想要的是从ngModel获取submitLogin()中的值,然后处理我想要的值。 Any suggestion? 有什么建议吗?

您可以从表单中获取值:

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

You can use reference variable to get access your ngModel 您可以使用引用变量来访问ngModel

<input type="text" [(ngModel)]="..." #name="ngModel">

And then use this variable if your template. 然后使用此变量,如果您的模板。

More details here: https://angular.io/docs/ts/latest/guide/template-syntax.html#ref-vars 更多细节: https//angular.io/docs/ts/latest/guide/template-syntax.html#ref-vars

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM