Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular 2: clear textarea from component class

Tags:

angular

I have a textarea with an option list. If one of the options is clicked and if enter key is pressed inside the text area the tag should be sent to the server and the text area should be cleared.

<ul>
<li *ngFor="let tag of tags">{{tag.tag}}</li>
<li><textarea #box (keyup)="searchTag(box.value)" (key.enter)="addTag(box.value)"></textarea>
    <ul>
        <li *ngFor="let tag of options" (click)="addTag(tag.tag)">{{tag.tag}}</li>
    </ul>
</li>

How could I clear the text area, before or after information is sent to server? The clear command should call from the addTag function.

like image 970
CordlessWool Avatar asked Nov 24 '16 13:11

CordlessWool


Video Answer


1 Answers

You can bind default value to textarea

<textarea [(ngModel)]="defaultValue"></textarea>

The component code:

export class TaggingComponent {
    defaultValue: string = '';

    addTag(value) {
      this.defaultValue = ''; 
    }
}

Be sure FormsModule has been imported to app.module.ts

import { FormsModule } from '@angular/forms';

@NgModule({
  imports:      [ 
    FormsModule
    ...
   ],
like image 111
Alex Kojin Avatar answered Nov 11 '22 12:11

Alex Kojin