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 Custom form control in angular 12

Create custom form control in angular 12 / 13

December 19, 2021March 19, 2022

In this article i will show you to use FormControl in child component. By default child component can not access the FormGroup and we can not use FormControl or FormControlName in parent child component approach. You can also read How to make reusable components in angular? for how child and…

Read More

How to submit multipart form data in angular reactive form ?

September 12, 2021October 31, 2021

In this tutorial, you will learn uploading a multipart reactive form in angular using HttpClient. In a webpage uploading files as multipart form data is a important aspect. If you are a new then you can read What is Angular and how to install angular ? So, for this tutorial…

Read More
Javascript ng init angular 2

ng init alternative in angular 2

October 6, 2021October 7, 2021

In angular js we have ng-init directive to process the expression or initialize the any variable in html view but in angular 2 we do not have any option to initialize variable or expression in html template, so in this tutorial we will learn how to implement ng init like…

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

  • 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 Resilience of Nature: How Forests Recover After Fires
  • Understanding Laravel Cookie Consent for GDPR Compliance
  • Understanding High Vulnerabilities: A Critical Overview of the Week of May 12, 2025
  • Installing a LAMP Stack on Ubuntu: A Comprehensive Guide
  • Understanding High Vulnerabilities: A Deep Dive into Recent Security Concerns
©2023 Readerstacks | Design and Developed by Readerstacks
Go to mobile version