[英]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)
}
}
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)
}
}
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.