Creating Angular Components
Angular components can be created manually or by using Angular CLI. Angular CLI is the recommended approach because it automatically creates the required component files and configuration.
What Does Creating a Component Mean?
Creating a component means creating a reusable part of the Angular user interface with its own logic, template and styling.
For example, in a Student Management application we can create a separate component for displaying student information.
Component Creation Flow
Angular Project
Open the Angular application.
Angular CLI
Generate a component using CLI.
Component Files
Angular creates component files.
Use Component
Use the component in the application.
Prerequisite
Before creating a component, make sure you have an Angular application and Angular CLI available.
ng version
Check Your Project
Run Angular CLI commands from inside your Angular project directory.
Creating a Component Using Angular CLI
The Angular CLI provides the ng generate component command to create a new component.
ng generate component student
The short version of the same command is:
ng g c student
Understanding the Command
Angular CLI
Angular CLI command-line interface.
Generate
Tells Angular CLI to generate something.
Component
Specifies that a component should be created.
Component Name
Name of the component being created.
Files Generated by Angular CLI
Depending on the Angular version and project configuration, Angular CLI generates the required files for the component.
student/
├── student.component.ts
├── student.component.html
├── student.component.css
└── student.component.spec.ts
Component TypeScript File
The TypeScript file contains the component class and its logic.
import { Component } from '@angular/core';
@Component({
selector: 'app-student',
templateUrl: './student.component.html',
styleUrl: './student.component.css'
})
export class StudentComponent {
studentName = 'Sam';
}
Component HTML Template
The HTML template defines the user interface of the component.
<h2>Student Details</h2>
<p>
Student Name: {{ studentName }}
</p>
Component CSS
The component stylesheet can be used to customize the appearance of the component.
h2 {
color: #4c1d95;
}
p {
font-size: 18px;
}
Using the Created Component
After creating the component, its selector can be used in another Angular template.
<h1>CIIT Student Management</h1>
<app-student></app-student>
Expected Output
CIIT Student Management
Student Details
Student Name: Sam
Creating a Component for Pradnya
We can create another component using the same Angular CLI command with a different name.
ng generate component student-profile
The component can then contain student profile information.
Reusing a Component
A component can be used wherever its selector is available in the Angular application.
Common Mistakes
1. Running the command outside the project
Make sure the terminal is opened inside the Angular project directory.
2. Using an incorrect selector
Make sure the selector used in the HTML matches the selector defined in the component metadata.
3. Incorrect component name
Use meaningful component names such as student, student-profile or student-list.
Complete Component Creation Flow
Summary
Angular components can be created easily using Angular CLI. The command ng generate component creates the required component files.
A component normally contains TypeScript logic, an HTML template and styling.
After creating a component, its selector can be used inside an Angular template to display the component.