Skip to content
Readerstacks logo Readerstacks
  • Home
  • Softwares
  • Angular
  • Php
  • Laravel
  • Flutter
Readerstacks logo
Readerstacks
reusable components in angular

How to make reusable components in angular?

Aman Jain, September 11, 2021October 4, 2022

Another rich feature of angular is reusability of component, Angular components are those components which we can reuse multiple times in a module or application. Angular reusable component can be dynamic and much flexible to send updates on any event .

Before start, I assume you well aware of installation of angular.

Follow the below steps to make the reusable components in angular:

Step 1 : Create a new project

Create a project reusable-component using ng new command.

ng new reusable-component

Step 2 : Create a Component

Next, Inside project root director create a new component using ng g command or manually.

 ng g component product-list

Above command will create the component in /src/app/product-list and it will also add the component in modules. Here, we will get 4 files component, html, Unit test and CSS. we can also create the component and all files manually with same name and need to import it in app.module.ts.

  1. product-list.component.html
  2. product-list.component.ts
  3. product-list.component.css
  4. product-list.component.spec.ts

In the product-list.component.ts, you will notice angular has added a decorator @Component which tells angular this is a component and component definition like template, selector(by which we can call our component in any other component), css and change detection strategy.

Component FIle : src\app\product-list.component.ts

import { Component, OnInit } from '@angular/core';

@Component({
  selector: 'app-product-list',
  templateUrl: './product-list.component.html',
  styleUrls: ['./product-list.component.css']
})
export class ProductListComponent implements OnInit {

  constructor() { }

  ngOnInit(): void {
  }

}

In the above code,selector: ‘app-product-list’ line is so important, because it’s our component selector by which we can use it anywhere in our module. Example:

<app-product-list></app-product-list>

Let’s make some changes in product-list.component.html

<p>This is product list component</p>

Step 3 : Use component in other component

Now, we have successfully created component and it’s time to add it in other component for example app.component.html

<app-product-list></app-product-list>

Using the above tag 3 times

<app-product-list></app-product-list>
<app-product-list></app-product-list>
<app-product-list></app-product-list>

It will render the same html of product-list component 3 times.

Now, Run the project with ng serve command and check the output.

Screenshot:

Screenshot 2021 09 11 at 10.10.20 AM
Reusable component

So, we have created simple reusable component but what if we wanted to make it dynamic or wanted to pass some variables from parent to child or child to parent.

Next, we have 2 requirements

  1. Communication from parent to child (Pass Parent variable in child )
  2. Child update the parent component.

To achieve above 2 scenarios, In angular there is multiple ways to communicate parent child or vise versa.

Sharing data between components

To share the data between component, here we are going to 2 decorators.

  1. @Input
  2. @Output

@Input is used to pass the variables from one component to another component.

EX: So, here i am going to add the decorators in product-list.component.ts

import { Component, EventEmitter, Input, OnInit, Output } from '@angular/core';

@Component({
  selector: 'app-product-list',
  templateUrl: './product-list.component.html',
  styleUrls: ['./product-list.component.css']
})
export class ProductListComponent implements OnInit {

  @Input() product:any={id:0,title:"Iphone 12",price:"$699",is_bought:false};
  @Output() onUpdate=new EventEmitter();
  constructor() { }

  ngOnInit(): void {
  }

  updateStatusToParent(p:any){
    p.is_bought=true;
    this.onUpdate.emit(p)
  }

}


Here, we added @Input and @output decorators and created a updateStatusToParent to update the parent component.

HTML product-list.component.html

<div class="row" >
    <p class="title">{{product.title}}</p>
    <p class="price">{{product.price}}</p>
    <button (click)="updateStatusToParent(product)" >{{product.is_bought?"Purchased":"Buy"}}</button>
</div>



Next, Let’s make it dynamic to show the different name and price

  • app.component.ts
  • app.component.html
<meta charset=”utf-8″><strong>app.component.ts</strong>
import { Component } from '@angular/core';

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent {

  products=[{index:0,title:'Samsung',price:'$499',is_bought:false},{index:1,title:'Iphone',price:'$799',is_bought:false},{index:2,title:'Mi x60',price:'$399',is_bought:false}];

  title = 'reusable-component';

  updateProduct(product:any){
    this.products[product.index]=product;
  }
}
<strong>app.component.html</strong>
<p *ngFor="let product of products">
  {{product.title}} is {{product.is_bought?"Purchased":"Not Purchased Yet"}}
</p>

<app-product-list *ngFor="let product of products" (onUpdate)="updateProduct($event)"  [product]="product"> </app-product-list>

(onUpdate) is output event which we defined in product-list component and [product] is input decorator share the data from parent to child.

Output:

Screenshot 2021 09 11 at 2.31.10 PM 1
Example of @Input and @Output decorator

I hope it will clear the topic of reusable components in angular. Download or clone the project

Download Code

Related

Javascript Angular

Post navigation

Previous post
Next post

Related Posts

Javascript How to convert JSON string to JSON object in angular

How to convert JSON string to JSON object in angular ?

April 13, 2022November 6, 2023

Sometimes in our application we want to convert JSON string to JSON object because the format of response is JSON string rather then JSON object, so in this tutorial i will show you to use JSON string to object and vise versa. JSON are used to communicate between client and…

Read More
Angular How to Use Interface in Angular with Example

How to Use Interface in Angular with Example?

August 6, 2022March 16, 2024

Interface in angular or in any programming languages is pattern which bind component or class to use same implementation across the modules. In this article we will cover the use of interface in angular with example. In Angular interface can be used on service, factory, component or any type of…

Read More
Javascript How to copy object without reference in angular

How to copy object without reference in angular ?

April 13, 2022April 14, 2022

In this tutorial we will learn to copy object without reference in angular. Objects and array are work on reference by address methodology means Objects and array keeps the address means if you change anything in the object or array from anywhere then it will change the value in original…

Read More

Aman Jain
Aman Jain

With years of hands-on experience in the realm of web and mobile development, they have honed their skills in various technologies, including Laravel, PHP CodeIgniter, mobile app development, web app development, Flutter, React, JavaScript, Angular, Devops and so much more. Their proficiency extends to building robust REST APIs, AWS Code scaling, and optimization, ensuring that your applications run seamlessly on the cloud.

Categories

  • Angular
  • CSS
  • Dart
  • Devops
  • Flutter
  • HTML
  • Javascript
  • jQuery
  • Laravel
  • Laravel 10
  • Laravel 11
  • Laravel 9
  • Mysql
  • Php
  • Softwares
  • Ubuntu
  • Uncategorized

Archives

  • August 2025
  • July 2025
  • June 2025
  • May 2025
  • April 2025
  • October 2024
  • July 2024
  • February 2024
  • January 2024
  • December 2023
  • November 2023
  • October 2023
  • July 2023
  • March 2023
  • November 2022
  • October 2022
  • September 2022
  • August 2022
  • July 2022
  • June 2022
  • May 2022
  • April 2022
  • March 2022
  • February 2022
  • January 2022
  • December 2021
  • November 2021
  • October 2021
  • September 2021
  • August 2021
  • July 2021
  • June 2021

Recent Posts

  • The Transformative Power of Education in the Digital Age
  • Understanding High Vulnerabilities: A Closer Look at the Week of July 14, 2025
  • Exploring Fresh Resources for Web Designers and Developers
  • The Intersection of Security and Technology: Understanding Vulnerabilities
  • Mapping Together: The Vibrant Spirit of OpenStreetMap Japan
©2023 Readerstacks | Design and Developed by Readerstacks
Go to mobile version