[英]How can I pass a value of an input into an event call in html?
I need to pass the value of my input to a variable which then I can used for my button (click).我需要将我的输入值传递给一个变量,然后我可以将其用于我的按钮(单击)。
My html我的html
<p class="mgRight">Second: <input type="number" min="1" value="10"/></p>
<button class="mgRight" type="button" (click)="callOnce(x)">Active Once After...</button>
My ts我的 ts
callOnce(x) {
var url = "api/url/" + x;
var xmlHttp = new XMLHttpRequest();
xmlHttp.open("GET", url, false);
xmlHttp.send(null);
return xmlHttp.responseText;
}
You need to bind your input to some property in the typescript.您需要将输入绑定到打字稿中的某些属性。
html html
<input type="number" min="1" value="10" [(ngModel)]="myProperty" />
ts ts
myProperty: number;
callOnce() {
var url = "api/url/" + this.myProperty;
var xmlHttp = new XMLHttpRequest();
xmlHttp.open("GET", url, false);
xmlHttp.send(null);
return xmlHttp.responseText;
}
This is just one way of doing it.这只是一种方法。 You should also look into HttpClient for making xhr requests.您还应该查看 HttpClient 以发出 xhr 请求。
Edit: myProperty (or whatever you call it) could also be a number if you are binding to a number input.编辑:如果您绑定到数字输入,则 myProperty(或您称之为的任何内容)也可以是一个数字。 Ultimately, it's a pure number anyway, because the javascript doesn't care about the "type" in your typescript.最终,无论如何它都是一个纯数字,因为 javascript 不关心打字稿中的“类型”。
If you don't want to store the value, you can even do the following如果您不想存储该值,您甚至可以执行以下操作
<input #yourInput type=“text”>
<button (click)=“callOnce(yourInput.value)”>
Creating a variable in the template and accessing to his value在模板中创建一个变量并访问他的值
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.