简体   繁体   English

如何将输入的值传递给 html 中的事件调用?

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

相关问题 如何单击 Bootstrap 4 输入组内的 HTML 输入来触发绑定的 Angular 事件 - How can I click an HTML input inside of a Bootstrap 4 input-group to fire a bound Angular event 如何在用户输入按键事件上调用多个请求? - How can I call multiple requests on user input key up event? 如何在 onSubmit 表单调用中将 select HTML 元素作为参数传递? - How can I pass a select HTML element as a parameter in a onSubmit form call? 如何将 TypeScript 接口传递给输入? - How can I pass TypeScript interfaces to an Input? 如何将@Input()装饰器值传递给它的html文件? - How to pass @Input() decorator value to its html file? 选择标签中的值更改时,如何调用函数并传递参数? 角度6 - how can I call a function and pass parameter when value in select tags changes? Angular 6 如何使用事件发射器调用返回值的函数,并在继续之前等待响应? - How can I use an Event Emitter to call a function that returns a value, and wait for the response before proceeding? 如何将枚举值作为变量传递给 Angular html 文件中的 function? - How can I pass an enum value as a variable to a function in the Angular html file? 如何将动态值传递给matIcon? - How can I pass a dynamic value to matIcon? Angular HTML 模板 - 如何使用数字输入的当前值作为数组切片的上限? - Angular HTML templating - How can I use the current value of a number input as the upper bound for my array slice?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM