Getting started with Angular TextBox component
26 Oct 20246 minutes to read
This section briefly explains about how to create a simple TextBox using Angular seed repository.
To get started quickly with Angular TextBox component, you can check out this video:
Dependencies
The following list of dependencies are required to use the TextBox component in your application.
|-- @syncfusion/ej2-angular-inputs
|-- @syncfusion/ej2-angular-base
|-- @syncfusion/ej2-inputs
|-- @syncfusion/ej2-base
Setup angular environment
Angular provides the easiest way to set angular CLI projects using Angular CLI
tool.
Install the CLI application globally to your machine.
npm install -g @angular/cli
Create a new application
ng new syncfusion-angular-textbox
By default, it install the CSS style base application. To setup with SCSS, pass –style=scss argument on create project.
Example code snippet.
ng new syncfusion-angular-textbox --style=scss
Navigate to the created project folder.
cd syncfusion-angular-textbox
Installing Syncfusion TextBox Package
Syncfusion packages are distributed in npm as @syncfusion
scoped packages. You can get all the Angular Syncfusion package from npm link.
Currently, Syncfusion provides two types of package structures for Angular components,
- Ivy library distribution package format
- Angular compatibility compiler(Angular’s legacy compilation and rendering pipeline) package.
Ivy library distribution package
Syncfusion Angular packages(>=20.2.36
) has been moved to the Ivy distribution to support the Angular Ivy rendering engine and the package are compatible with Angular version 12 and above. To download the package use the below command.
Add @syncfusion/ej2-angular-inputs
package to the application.
npm install @syncfusion/ej2-angular-inputs --save
Angular compatibility compiled package(ngcc)
For Angular version below 12, you can use the legacy (ngcc) package of the Syncfusion Angular components. To download the ngcc
package use the below.
Add @syncfusion/ej2-angular-inputs@ngcc
package to the application.
npm install @syncfusion/ej2-angular-inputs@ngcc --save
To mention the ngcc package in the package.json
file, add the suffix -ngcc
with the package version as below.
@syncfusion/ej2-angular-inputs:"20.2.38-ngcc"
Note: If the ngcc tag is not specified while installing the package, the Ivy Library Package will be installed and this package will throw a warning.
Adding TextBox to the application
- Modify the template as
ejs-textbox
tag inapp.component.ts
file to render theTextBox
component.
import {TextBoxModule} from '@syncfusion/ej2-angular-inputs'
import { Component } from '@angular/core';
@Component({
imports: [
TextBoxModule
],
standalone: true,
selector: 'app-root',
template: `<ejs-textbox placeholder="Enter Name"></ejs-textbox>`
})
export class AppComponent { }
Adding CSS reference
The following CSS files are available in ../node_modules/@syncfusion
package folder.
This can be referenced in [src/styles.css] using following code.
@import '../node_modules/@syncfusion/ej2-base/styles/material.css';
@import '../node_modules/@syncfusion/ej2-inputs/styles/material.css';
@import '../node_modules/@syncfusion/ej2-angular-inputs/styles/material.css';
The Custom Resource Generator (CRG) is an online web tool, which can be used to generate the custom script and styles for a set of specific components.
This web tool is useful to combine the required component scripts and styles in a single file.
Adding icons to the TextBox
You can create a TextBox with an icon by using the addIcon method within the created event. For detailed information, refer to the Groups section.
import { Component, ViewChild } from '@angular/core';
import { TextBoxComponent, TextBoxModule } from '@syncfusion/ej2-angular-inputs';
@Component({
imports: [TextBoxModule],
standalone: true,
selector: 'app-root',
template: `<ejs-textbox #textbox placeholder = "Enter Date" (created)="onCreate($event)"></ejs-textbox>`
})
export class AppComponent {
@ViewChild('textbox')
public textboxObj: TextBoxComponent;
public onCreate(args: any) {
(this.textboxObj as any).addIcon('append', 'e-icons e-input-popup-date');
}
}
Running the application
After completing the configuration required to render a basic TextBox, run the following command to display the output in your default browser.
ng serve
The following example illustrates the output in your browser.
import { Component, ViewChild} from '@angular/core';
import { TextBoxComponent, TextBoxModule } from '@syncfusion/ej2-angular-inputs';
@Component({
imports: [TextBoxModule],
standalone: true,
selector: 'app-root',
template: `<ejs-textbox #textbox placeholder = "Enter Date" (created)="onCreate($event)"></ejs-textbox>`
})
export class AppComponent {
@ViewChild('textbox')
public textboxObj: TextBoxComponent;
public onCreate(args: any) {
(this.textboxObj as any).addIcon('append', 'e-icons e-input-popup-date');
}
}
import { bootstrapApplication } from '@angular/platform-browser';
import { AppComponent } from './app.component';
import 'zone.js';
bootstrapApplication(AppComponent).catch((err) => console.error(err));
Floating label
The floating label TextBox floats the label above the TextBox after focusing, or filled with value in the TextBox.
You can create the floating label TextBox by using the floatLabelType API.
import { NgModule } from '@angular/core'
import { BrowserModule } from '@angular/platform-browser'
import {TextBoxModule} from '@syncfusion/ej2-angular-inputs'
import { Component } from '@angular/core';
@Component({
imports: [
TextBoxModule
],
standalone: true,
selector: 'app-root',
template: `<div class="wrap">
<h4>Floating label as auto</h4>
<div class='textboxes'>
<ejs-textbox placeholder="First Name" floatLabelType="Auto"></ejs-textbox>
</div>
<h4>Floating label as always</h4>
<div class='textboxes'>
<ejs-textbox placeholder="First Name" floatLabelType="Always"></ejs-textbox>
</div>
</div>`
})
export class AppComponent { }
import { bootstrapApplication } from '@angular/platform-browser';
import { AppComponent } from './app.component';
import 'zone.js';
bootstrapApplication(AppComponent).catch((err) => console.error(err));