Multiline in Angular TextBox component
26 Oct 202414 minutes to read
This feature allows the TextBox to accept one or more lines of text like address, description, comments, and more.
Create multiline TextBox
You can convert the default TextBox into the multiline TextBox by setting the multiline API value as true or pass HTML5 textarea as element to the TextBox.
The multiline TextBox allows you to resize it in vertical direction alone.
import { NgModule } from '@angular/core'
import { BrowserModule } from '@angular/platform-browser'
import { FormsModule } from '@angular/forms'
import { TextBoxModule } from '@syncfusion/ej2-angular-inputs'
import { Component, ViewChild } from '@angular/core';
@Component({
imports: [
FormsModule, TextBoxModule
],
standalone: true,
selector: 'app-container',
styleUrls: ['./index.css'],
template: `<div class="multiline">
<ejs-textbox [multiline]='true' value= 'Mr. Dodsworth Dodsworth, System Analyst, Studio 103, The Business Center' ></ejs-textbox>
</div>
`
})
export class AppComponent {
constructor() {
}
}
import { bootstrapApplication } from '@angular/platform-browser';
import { AppComponent } from './app.component';
import 'zone.js';
bootstrapApplication(AppComponent).catch((err) => console.error(err));
Implementing floating label
You can achieve the floating label behavior in the multiline TextBox by setting floatLabelType as ‘Auto’. The placeholder text act as floating label to the multiline TextBox. You can provide the placeholder text to the multiline TextBox either by using the placeholder property or placeholder attribute.
import { NgModule } from '@angular/core'
import { BrowserModule } from '@angular/platform-browser'
import { FormsModule } from '@angular/forms'
import { TextBoxModule } from '@syncfusion/ej2-angular-inputs'
import { Component, ViewChild } from '@angular/core';
@Component({
imports: [
FormsModule, TextBoxModule
],
standalone: true,
selector: 'app-container',
styleUrls: ['./index.css'],
template: `<label class="label">float label type auto</label>
<div class="multiline">
<ejs-textbox [multiline]='true' floatLabelType='Auto' placeholder='Enter your address' ></ejs-textbox>
</div>
<label class="label">float label type always</label>
<div class="multiline">
<ejs-textbox [multiline]='true' floatLabelType='Always' placeholder='Enter your address' ></ejs-textbox>
</div>
<label class="label">float label type never</label>
<div class="multiline">
<ejs-textbox [multiline]='true' floatLabelType='Never' placeholder='Enter your address' ></ejs-textbox>
</div>
`
})
export class AppComponent {
constructor() {
}
}
import { bootstrapApplication } from '@angular/platform-browser';
import { AppComponent } from './app.component';
import 'zone.js';
bootstrapApplication(AppComponent).catch((err) => console.error(err));
Auto resizing
By default, you can manually resize the multiline TextBox. But you can also create an auto resizing
multiline TextBox with both the initial and dynamic value change. It can be done by calculating the height of the textarea in the created event for initial value update and in the input event for dynamic value update of the auto resize multiline TextBox, as explained in the following code sample.
import { NgModule } from '@angular/core'
import { BrowserModule } from '@angular/platform-browser'
import { FormsModule } from '@angular/forms'
import { TextBoxModule } from '@syncfusion/ej2-angular-inputs'
import { Component, ViewChild } from '@angular/core';
import { TextBoxComponent } from '@syncfusion/ej2-angular-inputs';
@Component({
imports: [
FormsModule, TextBoxModule
],
standalone: true,
selector: 'app-container',
styleUrls: ['./index.css'],
template: `<div class="multiline">
<ejs-textbox #default [multiline]='true' value= 'Mr. Dodsworth Dodsworth, System Analyst, Studio 103, The Business Center'
floatLabelType='Auto' placeholder='Enter your address' (created)="createHandler($event)" (input)='inputHandler($event)' ></ejs-textbox>
</div>
`
})
export class AppComponent {
@ViewChild('default')
public textareaObj?: TextBoxComponent;
public createHandler(args: any): void {
(this.textareaObj as TextBoxComponent).addAttributes({rows: 1} as any);
(this.textareaObj as TextBoxComponent).element.style.height = "auto";
(this.textareaObj as TextBoxComponent).element.style.height = ((this.textareaObj as TextBoxComponent).element.scrollHeight)+"px";
}
public inputHandler(args: any): void {
(this.textareaObj as TextBoxComponent).element.style.height = "auto";
(this.textareaObj as TextBoxComponent).element.style.height = ((this.textareaObj as TextBoxComponent).element.scrollHeight)+"px";
}
}
import { bootstrapApplication } from '@angular/platform-browser';
import { AppComponent } from './app.component';
import 'zone.js';
bootstrapApplication(AppComponent).catch((err) => console.error(err));
Disable resizing
By default, the multiline TextBox is rendered with resizable. You can disable the resize of the multiline TextBox by applying the following CSS styles.
textarea.e-input,
.e-float-input textarea,
.e-float-input.e-control-wrapper textarea,
.e-input-group textarea,
.e-input-group.e-control-wrapper textarea {
resize: none;
}
import { NgModule } from '@angular/core'
import { BrowserModule } from '@angular/platform-browser'
import { FormsModule } from '@angular/forms'
import { TextBoxModule } from '@syncfusion/ej2-angular-inputs'
import { Component, ViewChild } from '@angular/core';
@Component({
imports: [
FormsModule, TextBoxModule
],
standalone: true,
selector: 'app-container',
styleUrls: ['./index.css'],
template: `<div class="multiline">
<ejs-textbox id='default' [multiline]='true' floatLabelType='Auto' placeholder='Enter your address' ></ejs-textbox>
</div>
`
})
export class AppComponent {
constructor() {
}
}
import { bootstrapApplication } from '@angular/platform-browser';
import { AppComponent } from './app.component';
import 'zone.js';
bootstrapApplication(AppComponent).catch((err) => console.error(err));
Limit the text length
By default, the text length of the multiline TextBox is unlimited. You can limit the text length by setting the maxLength
attribute using the addAttributes method.
import { NgModule } from '@angular/core'
import { BrowserModule } from '@angular/platform-browser'
import { ButtonModule } from '@syncfusion/ej2-angular-buttons'
import { FormsModule } from '@angular/forms'
import { TextBoxModule } from '@syncfusion/ej2-angular-inputs'
import { Component, ViewChild } from '@angular/core';
import { TextBoxComponent } from '@syncfusion/ej2-angular-inputs';
@Component({
imports: [
FormsModule, TextBoxModule, ButtonModule
],
standalone: true,
selector: 'app-container',
styleUrls: ['./index.css'],
template: `<label class="label">Add maxlength attribute through inline</label>
<div class="multiline">
<ejs-textbox [multiline]='true' maxlength='15' floatLabelType='Auto' placeholder='Enter your address' ></ejs-textbox>
</div>
<label class="label">Add maxlength attribute through addAttributes method</label>
<div class="multiline">
<ejs-textbox #default [multiline]='true' floatLabelType='Auto' placeholder='Enter your address' ></ejs-textbox>
</div>
<button ejs-button id=length (click)='clickHandler($event)'>Add max length</button>
`
})
export class AppComponent {
@ViewChild('default')
public textareaObj?: TextBoxComponent;
public clickHandler(args: any) {
(this.textareaObj as TextBoxComponent).addAttributes({maxlength: 15} as any);
}
}
import { bootstrapApplication } from '@angular/platform-browser';
import { AppComponent } from './app.component';
import 'zone.js';
bootstrapApplication(AppComponent).catch((err) => console.error(err));
Count characters
You can show the number of characters entered inside the textarea by calculating the character count in the input event of multiline TextBox. The character count is updated while entering or deleting any character inside the textarea. The character count shows how many characters can be entered or left to be entered.
import { NgModule } from '@angular/core'
import { BrowserModule } from '@angular/platform-browser'
import { FormsModule } from '@angular/forms'
import { TextBoxModule } from '@syncfusion/ej2-angular-inputs'
import { Component, ViewChild } from '@angular/core';
import { TextBoxComponent } from '@syncfusion/ej2-angular-inputs';
@Component({
imports: [
FormsModule, TextBoxModule
],
standalone: true,
selector: 'app-container',
styleUrls: ['./index.css'],
template: `<div class="multiline">
<ejs-textbox #default [multiline]='true' maxlength='25' floatLabelType='Auto' placeholder='Enter your address' (input)='inputHandler($event)' ></ejs-textbox>
<span id='numbercount'></span>
</div>
`
})
export class AppComponent {
@ViewChild('default')
public textareaObj?: TextBoxComponent;
public inputHandler(args: any): void {
let word: any, addresscountRem, addressCount: string;
word = this.textareaObj?.element.value;
addressCount = word.length;
addresscountRem = document.getElementById('numbercount');
(addresscountRem as HTMLElement).textContent = addressCount+"/25";
}
}
import { bootstrapApplication } from '@angular/platform-browser';
import { AppComponent } from './app.component';
import 'zone.js';
bootstrapApplication(AppComponent).catch((err) => console.error(err));