简体   繁体   English

如何在 Angular 中使用 Ngmodel

[英]How to use Ngmodel in Angular

I want to save text to text file from text area by using a blob.我想使用 blob 将文本从文本区域保存到文本文件。

in.html in.html

   <h1>{{title}}</h1>
<form action="textarea1" method="post" name="test">
<br /><br/>
<textarea type = "Text" #box2 [(ngModel)]= "textareabox" placeholder="type something here.." (keyup) = "getData(box2.value)" style="width: 500px;height: 700px;" ></textarea>

<br /><br/>

<button (click)="saveAsTextFile()"  type="submit" style="width: 100px;height:30px" >Send</button>

</form>

in.ts in.ts

export class AppComponent {
  title = 'Important Event in Angular';
  getData(val:any) {
    console.warn(val);
  }
    textareabox ="";
    txtData:any = this.textareabox;
 
    constructor() { }
    
    saveAsTextFile(){
    
      var data = new Blob([this.txtData], {type: 'text/plain'});
 
      let url = window.URL.createObjectURL(data);
 
      let a = document.createElement('a');
      document.body.appendChild(a);
 
      a.setAttribute('style', 'display: none');
      a.href = url;
      a.download = 'Essay.txt';
      a.click();
      window.URL.revokeObjectURL(url);
      a.remove();
    }
}

*** I use ngmodel in textarea but I can't save text that I type in Textarea. *** 我在 textarea 中使用 ngmodel,但无法保存在 Textarea 中键入的文本。 *** ***

The "txtData" variable equals an empty string. “txtData”变量等于一个空字符串。 You should use "textareabox" in the "saveAsTextFile" function:您应该在“saveAsTextFile”function 中使用“textareabox”:

saveAsTextFile(){
    
      var data = new Blob([this.textareabox], {type: 'text/plain'});
      ...

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM