简体   繁体   English

当我单击 Angular 中的按钮时,如何在输入字段中输入我写的内容?

[英]How to type out what I write in input field when I click a button in Angular?

So I want to write something in the input textfield and then press the button and show it here <p>Hello {{inputValue}}</p> .所以我想在输入文本字段中写一些东西,然后按下按钮并在此处显示<p>Hello {{inputValue}}</p> I DO NOT want to use keyup for this.我不想为此使用 keyup。 I am also saving it in localstorage which works right now but not if I remove keyup.我也将它保存在 localstorage 中,它现在可以工作,但如果我删除 keyup 就不行了。

My .ts file:我的 .ts 文件:

  selector: 'app-dialouge',
  templateUrl: './dialouge.component.html',
  styleUrls: ['./dialouge.component.css']
})

export class DialougeComponent implements OnInit {
  inputValue: string;
  onKeyUp(event){
    this.inputValue = event.target.value;
  }

  saveBtn(): void {
    console.log("btn clicked: " + this.inputValue);
    window.localStorage.setItem("inputValue", this.inputValue);
    window.localStorage.getItem("inputValue");
  };
  clearStorage(){
    localStorage.clear();
    console.log("ls cleared");
  }


  constructor() { }

  ngOnInit(): void {

  }

}

my template file:我的模板文件:

<input type="text" (keyup)="onKeyUp($event)" />
<button (click)="saveBtn()">save</button>
<button (click)="clearStorage()">clear</button>

<p>Hello {{inputValue}}</p>

You can use ngModel instead of keyup into input .您可以在input使用ngModel而不是keyup

change your HTML like below,像下面一样改变你的 HTML,

<input type="text" [(ngModel)]="inputValue" />
<button (click)="saveBtn()">save</button>
<button (click)="clearStorage()">clear</button>

<p>Hello {{inputValue}}</p>

Remove onKeyUp method from your ts file.从 ts 文件中删除onKeyUp方法。

You should use [(ngModel)] to change it with input , because it's more proper to use [(ngModel)] .您应该使用[(ngModel)]input更改它,因为使用[(ngModel)]更合适。 keyup fires when you released your finger from keyboard.当您从键盘上松开手指时, keyup会触发。

<input type="text" [(ngModel)]="inputValue" />
<button (click)="saveBtn()">save</button>
<button (click)="clearStorage()">clear</button>

<p>Hello {{afterButtonPressedValue}}</p>

Create another value to show and type: inputValue for typing and afterButtonPressedValue for showing in template.创建另一个值显示和输入: inputValue用于打字和afterButtonPressedValue在模板显示。

And your ts will be:你的 ts 将是:

  selector: 'app-dialouge',
  templateUrl: './dialouge.component.html',
  styleUrls: ['./dialouge.component.css']
})

export class DialougeComponent implements OnInit {
  inputValue: string;
  afterButtonPressedValue = '';

  saveBtn(): void {
    console.log("btn clicked: " + this.inputValue);
    window.localStorage.setItem("inputValue", this.inputValue);
    window.localStorage.getItem("inputValue");
    this.afterButtonPressedValue = this.inputValue;
  };
  clearStorage(){
    localStorage.clear();
    console.log("ls cleared");
    this.afterButtonPressedValue = '';
  }


  constructor() { }

  ngOnInit(): void {

  }

}

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

相关问题 按钮显示为禁用,直到我在 Angular 中点击输入 - Button appears disabled until i click out of input in Angular 我需要创建一个 Angular 指令来清除图标按钮单击输入字段中输入的内容 - I need to create a Angular directive to clear content entered in input field in icon button click 在 Angular 9 中单击按钮时自动聚焦在输入字段上 - Autofocus on an input field on button click in angular 9 写出我在我的<select>以角度列表 - Write out what i select in my <select> list in angular 当我在输入类型=“数字”中输入&#39;e&#39;字符时,数字将被放置在角度字段中 - When I put 'e' character in input type=“number ” the number is put in the field angular 我如何获得 Angular 输入字段值? - How I get Angular input field value? 不使用表单进行验证如何禁用角度按钮如果<input>字段为空 - without using form for validation How can I disabled a button in angular if <input> field is empty 如何在 angular 中找到输入的类型? - How do I find the type of an input in angular? Angular:当我单击一个按钮两次时,我如何 go 到不同的路线? - Angular: How can I go to a different route when I click a button twice? 如何显示日期<input type="date">使用 Angular 时的字段 - How to display a date in <input type = "date"> field when using Angular
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM