Table of contents
1.
Introduction
2.
Creating a Component in Angular 8
3.
Using Components in Angular 8
4.
Frequently Asked Questions
4.1.
What is the purpose of a component in Angular?
4.2.
How do you pass data to a child component in Angular?
4.3.
Can a component be reused in multiple places?
5.
Conclusion
Last Updated: Jan 21, 2025
Medium

Components in Angular 8

Author Sinki Kumari
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

Angular is a popular framework for building web applications. It allows developers to create highly interactive and dynamic user interfaces. At the heart of Angular applications are components, which act as building blocks for the UI. Components help organize the application into smaller, reusable pieces, making development efficient and manageable.

Components in Angular 8

In this article, we will discuss components in Angular 8, how to create them, use them, and work with child components.

Creating a Component in Angular 8

Components in Angular 8 are created using the Angular CLI (Command Line Interface). Each component consists of three key parts:

  1. HTML Template: Defines the structure and layout of the view.
     
  2. TypeScript File: Contains the logic and data binding of the component.
     
  3. CSS File: Handles the styling of the component.


Here is how you can create a new component:

1. Open your Angular project directory in the terminal.
 

2. Run the following command to create a new component:

ng generate component component-name


Replace component-name with the desired name of your component. For example:

ng generate component user-profile


This command will create a new folder named user-profile containing the following files:

  • user-profile.component.ts
     
  • user-profile.component.html
     
  • user-profile.component.css
     
  • user-profile.component.spec.ts (for testing purposes)
     

3. The user-profile.component.ts file will look like this:

import { Component } from '@angular/core';
@Component({
  selector: 'app-user-profile',
  templateUrl: './user-profile.component.html',
  styleUrls: ['./user-profile.component.css']
})
export class UserProfileComponent {
  userName: string = 'John Doe';
  userAge: number = 25;
}


4. Edit the HTML template (user-profile.component.html) to define the component’s structure:

<div class="profile">
  <h2>User Profile</h2>
  <p>Name: {{ userName }}</p>
  <p>Age: {{ userAge }}</p>
</div>


5. Add some styles in user-profile.component.css:

.profile {
  border: 1px solid #ccc;
  padding: 10px;
  border-radius: 5px;
  width: 200px;
}
h2 {
  color: #007bff;
      }

Using Components in Angular 8

Once a component is created, it can be used within other components by referencing its selector. Here’s how to use the UserProfileComponent:

  1. Open the parent component’s HTML file (e.g., app.component.html).
     
  2. Add the component’s selector as a custom HTML tag:
<app-user-profile></app-user-profile>


When you run the application using ng serve, the output will display the user profile data in the browser.

Frequently Asked Questions

What is the purpose of a component in Angular?

Components are used to divide an application into smaller, reusable parts. They simplify development by organizing the UI and logic into separate, manageable pieces.

How do you pass data to a child component in Angular?

Data is passed to a child component using the @Input() decorator in the child component and binding properties in the parent template.

Can a component be reused in multiple places?

Yes, components can be reused throughout the application by including their selector wherever required.

Conclusion

Components in Angular 8 are the backbone of any Angular application. They allow developers to create reusable, maintainable, and organized UI elements. By understanding how to create, use, and manage components, we can build dynamic and scalable applications.

Live masterclass