Thursday, May 30, 2024
AngularAngular 17Bootstrap 5

How to create reusable components in angular 17?

How to create reusable components in angular 17?

Hello friends, welcome back to my blog. Today in this blog post, I am going to show you, How to create reusable components in angular 17?

Live Demo

Guys inside this I have used angular input output event emitter for Sharing data between child and parent directives and components.

Angular 17 came and if you are new then you must check below link:

  1. Angular 17 Basic Tutorials
  2. Datatable

Friends now I proceed onwards and here is the working code snippet and please use carefully this to avoid the mistakes:

1. Firstly friends we need fresh angular 17 setup and for this we need to run below commands but if you already have angular 17 setup then you can avoid below commands. Secondly we should also have latest node version installed on our system:

npm install -g @angular/cli 

ng new angulardatatable //Create new Angular Project 

cd angulardatatable // Go inside the Angular Project Folder

ng g c datalist

2. Guys for bootstrap 5 I used the cdn’s for quick output or try this to add bootstrap into angular application click here:

<!doctype html>
<html lang="en">
  <meta charset="utf-8">
  <base href="/">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="icon" type="image/x-icon" href="favicon.ico">

  <!-- Google Font -->
  <link href=";400;600;700;800;900&display=swap"

  <!-- Css Styles -->
  <link rel="stylesheet" href="" type="text/css">
  <link rel="stylesheet" href="" type="text/css">

  <script src=""></script>
  <script src=""></script>

3. Now friends we just need to add below code into src/app/app.component.ts file to create dummy data:


export class AppComponent {

totalCount = 0;

  data1 = [{
    name : 'Sam Johnson',
    dept : 'Electrical'
    name : 'Roy Thomas',
    dept : 'Mechanical'
    name : 'Jim Lasker',
    dept : 'Medical'

  data2 = [{
    name : 'Johnson',
    dept : 'Physics'
    name : 'Thomas',
    dept : 'Chemistry'
    name : 'Lasker',
    dept : 'Biology'

  calCount(count: any){
    this.totalCount = this.totalCount + count;


4. Now friends we need to add below code into app.component.html file to get final output on web browser:

<div class="container">
    <div class="row">
        <div class="col-sm-12">
          <app-datalist (calCount)="calCount($event)" [data]="data1" [showCount]="true"></app-datalist>
      <div class="row">
        <div class="col-sm-12">
          <app-datalist (calCount)="calCount($event)" [data]="data2" [showCount]="true"></app-datalist>
      <div class="row float-right">
        <div class="col-sm-12 count-text">
          Total Data Count ::  <strong>

5. Guys now we need to add below inside src/app/datalist/datalist.component.ts file to make reusable component:

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

  selector: 'app-datalist',
  standalone: true,
  imports: [CommonModule],
  templateUrl: './datalist.component.html',
  styleUrl: './datalist.component.css'
export class DatalistComponent {
  @Input() data :any = [];
  @Input() showCount = false;
  @Output() calCount = new EventEmitter<Number>();

  constructor() { }

  ngOnInit(): void {

6. Guys now we need to add below inside src/app/datalist/datalist.component.html file to make reusable html:

<div class="container p-2 margin-30">
    <div class="card">
      <div class="card-body">
        <ol class="list-group list-group-numbered">
          <li *ngFor="let item of data" class="list-group-item d-flex justify-content-between align-items-start">
            <div class="ms-2 me-auto">
              <div class="fw-bold font-fam">{{item.dept}}</div>
            <span *ngIf="showCount" class="badge bg-primary rounded-pill">14</span>

Now we are done friends and please run ng serve command and if you have any kind of query then please do comment below.

Note: Friends, I just tell the basic setup and things, you can change the code according to your requirements. For better understanding must watch video above.

I will appreciate that if you will tell your views for this post. Nothing matters if your views will be good or bad.



the authortherichpost
Hello to all. Welcome to Myself Ajay Malhotra and I am freelance full stack developer. I love coding. I know WordPress, Core php, Angularjs, Angular 14, Angular 15, Angular 16, Angular 17, Bootstrap 5, Nodejs, Laravel, Codeigniter, Shopify, Squarespace, jQuery, Google Map Api, Vuejs, Reactjs, Big commerce etc.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.