Home > OS >  Angular binding different elements
Angular binding different elements

Time:07-17

I have got simple question: how to create binding between button and textarea? I see that like:

html

<textarea [value]="test"> <button (click)="onclick($event)>

ts

onclick(event: Event) {
  this.test = ($event.HowToChooseTarget as HTMLInputElement).value;
}

As you can see, the problem is to choose target element there. How to do this? Thank you for any help.

CodePudding user response:

There are multiple ways to achieve this.

  1. With help of template ref variable

    <textarea #test [value]="test"> <button (click)="onclick(test.value)>
    
  2. Use ngModel and directly access the value in your component file with "this.test"

    <textarea [(ngModel)]="test"> <button (click)="onclick($event)>

  • Related