0

I am a very new user of Angualr and currently using Angular 10. I want the textarea field that should be empty after click on the clear button. But this code is not working that I want to do. Could someboday help me. Thanks.

HTM file

<button (click)="ClearField()" type="button">clear field</button> 
<textarea class="form-control  rows=10 cols=50 cstm-textarea" #box (keyup)="OnUpdateTextArea(box.value)">
</textarea>

.ts file

export class AppComponent {
  
  str = '';

  OnUpdateTextArea(value: string) {
    this.str = value;
    console.log("text area value:", this.str);
    
  }

  ClearField() {
    this.str = '';
  }
}
Atika.Akmal
  • 11
  • 1
  • 3

2 Answers2

0

Believe binding can fix this problem.

<button (click)="ClearField()" type="button">clear field</button> 
<textarea [(ngModel)]="str" class="form-control  rows=10 cols=50 cstm-textarea">
</textarea>

and ts I believe can stay the same.

You should look into one way and two way binding

0

The solution of this problem:

<button (click)="ClearField(box)" type="button">clear field</button> 
<textarea class="form-control  rows=10 cols=50 cstm-textarea"  #box (keyup)="OnUpdateTextArea(box.value)">
</textarea>
Atika.Akmal
  • 11
  • 1
  • 3