Get user input from textarea

170,871

Solution 1

<pre>
  <input type="text"  #titleInput>
  <button type="submit" (click) = 'addTodo(titleInput.value)'>Add</button>
</pre>

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

Solution 2

I think you should not use spaces between the [(ngModel)] the = and the str. Then you should use a button or something like this with a click function and in this function you can use the values of your inputfields.

<input id="str" [(ngModel)]="str"/>
<button (click)="sendValues()">Send</button>

and in your component file

str: string;
sendValues(): void {
//do sth with the str e.g. console.log(this.str);
}

Hope I can help you.

Solution 3

Tested with Angular2 RC2

I tried a code-snippet similar to yours and it works for me ;) see [(ngModel)] = "str" in my template If you push the button, the console logs the current content of the textarea-field. Hope it helps

textarea-component.ts

import {Component} from '@angular/core';

@Component({
  selector: 'textarea-comp',
  template: `
      <textarea cols="30" rows="4" [(ngModel)] = "str"></textarea>
      <p><button (click)="pushMe()">pushMeToLog</button></p>
  `
})

  export class TextAreaComponent {
    str: string;

  pushMe() {
      console.log( "TextAreaComponent::str: " + this.str);
  }
}

Solution 4

Just in case, instead of [(ngModel)] you can use (input) (is fired when a user writes something in the input <textarea>) or (blur) (is fired when a user leaves the input <textarea>) event,

<textarea cols="30" rows="4" (input)="str = $event.target.value"></textarea>

Solution 5

Here is full component example

import { Component } from '@angular/core';

@Component({
  selector: 'app-text-box',
  template: `
        <h1>Text ({{textValue}})</h1>
        <input #textbox type="text" [(ngModel)]="textValue" required>
        <button (click)="logText(textbox.value)">Update Log</button>
        <button (click)="textValue=''">Clear</button>

        <h2>Template Reference Variable</h2>
        Type: '{{textbox.type}}', required: '{{textbox.hasAttribute('required')}}',
        upper: '{{textbox.value.toUpperCase()}}'

        <h2>Log <button (click)="log=''">Clear</button></h2>
        <pre>{{log}}</pre>`
})
export class TextComponent {

  textValue = 'initial value';
  log = '';

  logText(value: string): void {
    this.log += `Text changed to '${value}'\n`;
  }
}
Share:
170,871
Maryam
Author by

Maryam

Computer Information Systems Student who Will be graduated in just 9 Weeks !

Updated on June 02, 2021

Comments

  • Maryam
    Maryam almost 3 years

    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 ?

  • Michael L.
    Michael L. over 6 years
    In which scope can I use the titleInput ID? I had a workin example like this and rearranged code for styling issues but know it says: Cannot read 'value' of undefined
  • Always_a_learner
    Always_a_learner almost 6 years
    What if we want to an input field where we want to have prefilled value as well we want to access that input field in component. Can we do that?
  • Mohammed Shoaib
    Mohammed Shoaib over 3 years
    This is more of a workaround of using the input tag when the author wants the textarea tag.
  • Mohammed Shoaib
    Mohammed Shoaib over 3 years
    Isn't it possible to bind the value without using (input)?