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 Angular Blur Event on Input Example

Angular Blur Event on Input Example

September 29, 2022March 16, 2024

Angular Blur Event on Input is an Angular event binding it triggers when user removes the focus from the input element. Input events in angular can be handled using input and output decorators and in this tutorial we will learn Angular Blur Event on Input with Example. In core javascript…

Read More
Javascript Angular KeyDown Event on Input Example

Angular KeyDown Event on Input Example

September 22, 2022March 16, 2024

Angular KeyDown Event on Input is an Angular event binding it triggers when user interacts with DOM like text based input element and when we key down the input. Input events in angular can be handled using input and output decorators and in this tutorial we will learn Angular KeyDown…

Read More
Javascript Password and confirm password validation in angular

Password and confirm password validation in angular ?

September 11, 2021November 9, 2023

Angular provides built-in library for validation but it doesn’t have built in validation for Password and confirm password validation in angular. so in this tutorial i am going to explain how can we make custom validation for this same as angular built-in validations. Confirm password most useful utility when we…

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

  • 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

  • Understanding High Vulnerabilities: A Deep Dive into Recent Security Concerns
  • Understanding High Vulnerabilities in Software: A Week of Insights
  • Blocking Spam Requests with LaraGuard IP: A Comprehensive Guide
  • Enhancing API Development with Laravel API Kit
  • Exploring the Future of Web Development: Insights from Milana Cap
©2023 Readerstacks | Design and Developed by Readerstacks
Go to mobile version