简体   繁体   English

单击按钮时如何获取我在文本框中输入的值?

[英]How to get the value that I entered in the textbox when click the button?

.HTML .HTML

<input type="email" #inputvalue>
<button type="submit" (submit)="onSubmit(inputvalue.value)">Verify</button>

.TS .TS

export class LoginComponent implements OnInit {
   inputvalue: any;

   onSubmit(inputvalue: any) {
   this.inputvalue = inputvalue;
   console.log(this.inputvalue)
  }
}
  • I want to get the values that I entered in textbox to inputvalue variable after click the verify button.单击验证按钮后,我想将在文本框中输入的值输入到inputvalue变量中。

As Anurag has mentioned in his comment you should go and learn a bit about Angular .正如 Anurag 在他的评论中提到的那样,您应该 go 并了解一些有关Angular的知识。 There are two ways you can achieve this:有两种方法可以实现这一目标:

HTML HTML

<input type="email" [(ngModel)]="emailValue">
<input type="email" #email>
<button (click)="submit()">Verify</button>

TS TS

import { Component, ViewChild, ElementRef } from '@angular/core';

@Component({
  selector: 'my-app',
  templateUrl: './app.component.html',
  styleUrls: [ './app.component.css' ]
})
export class AppComponent  {

  emailValue: string;
  @ViewChild('email', {static: true}) email: ElementRef;

  submit() {
   console.log(this.emailValue)
   console.log(this.email.nativeElement.value)
  }
}

Stackblitz 堆栈闪电战

Also instead of ngModel TWO way binding you can use formControlName = "emailValue" , You just need a parent FormGroup in html.此外,您可以使用formControlName = "emailValue"代替 ngModel 两种方式绑定,您只需要 html 中的父 FormGroup。 You can read about using FormGroup online.您可以在线阅读有关使用 FormGroup 的信息。 It is better and maintainable as compared to ngModel与 ngModel 相比,它更好且可维护

暂无
暂无

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

相关问题 当我单击“计算”按钮时,它如何获取每个A,B框中的值,并将这些值传递给服务器以计算结果并发出Alert()? - When I click the “Calculate” button, how can it get the values in each box A,B and pass those value to server to calculate and alert() the result? 如何获得按钮单击时的当前* ngFor值? - How to get the current *ngFor value on Button click? 当我使用 *ngFor 时如何保存单选按钮点击? - How to save radio button click, when I use *ngFor? 当单击按钮值未在Angular JS中更新时 - When click button value not update in angular js 如何在 selenium webdriver , Js 中获取文本框的值 - How to get value of textbox in selenium webdriver , Js 当我右键单击intellij时,我没有得到JS文件的运行按钮 - When I right click on intellij, I do not get the run button for a JS File 我想从HTML文档中获取文本框值,并在节点JS中使用Json文件按下提交按钮时发布它们 - I want to get my textbox values from HTML document and post those when submit button is pressed using Json file in node JS 当我单击链接的按钮时无法获取 /to-do.html(节点 js) - Cannot GET /to-do.html when I click on button linked (Node js) 当我单击 github 中的合并请求按钮时,是否会执行预合并提交挂钩? - Does the pre-merge commit hook get executed when I click merge request button in github? 如何从按钮获取数据值? - How do I get the data value from a button?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM