Get user input from textarea

Maryam picture Maryam · Jun 18, 2016 · Viewed 158.6k times · Source

I'm new to angular2. I want to store user input from a text area in a variable in my component so I can apply some logic to this input. I tried ngModel but it doesn't work. My code for the textarea:

<textarea cols="30" rows="4" [(ngModel)] = "str"></textarea>

And inside my component:

str: string;
//some logic on str

But I don't get any value in str inside my component. Is there an error with the way I'm using ngModule ?

Answer

Arthur  Qocharyan picture Arthur Qocharyan · Oct 10, 2016
<pre>
  <input type="text"  #titleInput>
  <button type="submit" (click) = 'addTodo(titleInput.value)'>Add</button>
</pre>

{
  addTodo(title:string) {
    console.log(title);
  }
}