简体   繁体   English

如何将输入 keyup 值绑定到 component.ts 中的变量?

[英]How to bind input keyup value to a variable in component.ts?

I have the following in my component.ts file:我的component.ts文件中有以下内容:

@Component({
  selector: 'app-loop-back',
  template: `
    <input #box (keyup)="0">
    <p>{{box.value}}</p>
  `
})
export class LoopbackComponent implements OnInit{ 
    sampleString: string;

    contructor(){}

    ngOnInit(): void {}
}

Basically whatever I typed in the <input #box (keyup)="0"> will be shown immediately in the {{box.value}} .基本上,我在<input #box (keyup)="0">中输入的任何内容都会立即显示在{{box.value}}中。 As this is purely in the html file, I am wondering how can I bind the input {{box.value}} to sampleString: string;由于这纯粹是在 html 文件中,我想知道如何将输入{{box.value}}绑定到sampleString: string; in my component.ts so that I can use it somewhere else.在我的component.ts中,以便我可以在其他地方使用它。

you can bind the method on change event in input element and update the string value您可以在输入元素中绑定change事件的方法并更新字符串值

class file class 文件

    import { Component } from '@angular/core';

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

       sampleString: string;

       updateBox(e) {
         this.sampleString = e.target.value; 
       }

    }

Template File模板文件

<input #box (keyup)="0" (input)="updateBox($event)">
  <p>{{box.value}}</p>
<p>This is sampleString Value: {{sampleString}} </p>

You can use angular two way binding as shown below您可以使用 angular 两种方式绑定如下图

 @Component({ selector: 'app-loop-back', template: ` <input [(ngModel)]="sampleString"> <p>{{sampleString}}</p> ` }) export class LoopbackComponent implements OnInit{ sampleString: string; contructor(){} ngOnInit(): void {} }

Try like this if you want to use only HTML an not 2 way binding,如果您只想使用 HTML 而不是 2 路绑定,请尝试这样,

<input #box (keyup)="sampleString = $event.target.value">

Working Demo 工作演示

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

相关问题 如何绑定选择/选项值以在service.ts中而不是component.ts中获得请求功能(角度8) - How to bind selected/option value to get request function in service.ts and not component.ts (Angular 8) 如何将 HTML 中的 {{value}} 传递给 component.ts - How to pass {{value}} in HTML to component.ts 如何从下拉列表和 *ngFor 中创建的文本中获取 component.ts 中的输入值 - How to get input value in component.ts from dropdown and text created within *ngFor in table Select 框未绑定到 component.ts - Select box in does not bind to component.ts angular 2 4 5-如何在component.ts中定义对象变量 - angular 2 4 5 - how to define object variable in component.ts 如何在 Angular component.ts 中的 function 之外声明 HTMLElement 变量? - How to declare HTMLElement variable outside of a function in Angular component.ts? 如何从component.ts获取Material DatePicker的选定值 - How to get selected value of Material DatePicker from component.ts 在 component.ts Angular 中使用可变颜色 - Use variable color in component.ts Angular 如何将 component.ts 中的变量发送到 service.ts 进行身份验证 Angular - How to send variable in component.ts to service.ts for authentication Angular 如何从另一个 Component.ts 访问一个 Component.ts(在同一个 app.component.html 中) - How to access a Component.ts from another Component.ts (in the same app.component.html)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM