Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiline text area in angular material 2

How do I wrap text and have support for multiple line, using angular material 2 and angular.

<md-input-container fxFlex="18" fxFlexOffset="1">
      <textarea [(ngModel)]="Comments" name="Comments" mdInput placeholder="Comments" ></textarea>
        <md-error>This field is required</md-error>
    </md-input-container>

Wrapping text mean, text should be appear multi line if content is longer than width.

like image 368
Ashish Avatar asked Oct 31 '17 15:10

Ashish


People also ask

How do you wrap text in mat form field?

To wrap text in an Input Text Field, select 'textarea' in the properties palette. To wrap text in a regular text field, change it to multi line.

How do I make multiple lines in a text box in HTML?

To create a multi-line text input, use the HTML <textarea> tag. You can set the size of a text area using the cols and rows attributes. It is used within a form, to allow users to input text over multiple rows.

What is MatInput?

Matinput is an Angular directive that primarily allows input and text area elements to work with a form field. With this, you can display placeholders perfectly, add custom error messages, a clear button, specify the maximum length of the text or add prefixes and suffixes for a seamless user experience.

What is MatFormFieldControl?

MatFormFieldControl. An interface which allows a control to work inside of a MatFormField .


2 Answers

  • Doesn't textarea elements have multi-line support? Did you mean setting the rows for the textarea? Just use the native rows attribute and set it to the value (as a number) of your choice.

    <md-input-container fxFlex="18" fxFlexOffset="1">
      <textarea [(ngModel)]="Comments" name="Comments" mdInput placeholder="Comments" rows="3"></textarea>
        <md-error>This field is required</md-error>
    </md-input-container>
    
  • As for wrapping the text, I don't really understand what you mean. Could you please elaborate on that?

like image 67
Edric Avatar answered Sep 28 '22 02:09

Edric


Not the exact answer for the question but since this is the best match in google search, to set rows for a textarea in angular apperantly we need to usecdkTextareaAutosize, cdkAutosizeMinRowsattributes .

    <mat-form-field>
      <textarea matInput placeholder="Your Message" formControlName="message" cdkTextareaAutosize
        cdkAutosizeMinRows="6"></textarea>
    </mat-form-field>

like image 38
mkb Avatar answered Sep 28 '22 03:09

mkb