[英]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?
[英]How to get the value that I entered in the textbox when click the button?
.HTML
<input type="email" #inputvalue>
<button type="submit" (submit)="onSubmit(inputvalue.value)">Verify</button>
.TS
export class LoginComponent implements OnInit {
inputvalue: any;
onSubmit(inputvalue: any) {
this.inputvalue = inputvalue;
console.log(this.inputvalue)
}
}
正如 Anurag 在他的評論中提到的那樣,您應該 go 並了解一些有關Angular的知識。 有兩種方法可以實現這一目標:
HTML
<input type="email" [(ngModel)]="emailValue">
<input type="email" #email>
<button (click)="submit()">Verify</button>
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)
}
}
此外,您可以使用formControlName = "emailValue"代替 ngModel 兩種方式綁定,您只需要 html 中的父 FormGroup。 您可以在線閱讀有關使用 FormGroup 的信息。 與 ngModel 相比,它更好且可維護
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.