Categories

Sunday, April 28, 2024
#919814419350 therichposts@gmail.com
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">
<head>
  <meta charset="utf-8">
  <title>Jassa</title>
  <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="https://fonts.googleapis.com/css2?family=Nunito+Sans:wght@300;400;600;700;800;900&display=swap"
  rel="stylesheet">

  <!-- Css Styles -->
  <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.0-beta1/dist/css/bootstrap.min.css" type="text/css">
  <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/boxicons@latest/css/boxicons.min.css" type="text/css">


</head>
<body>
  <app-root></app-root>
  <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.0.0-beta1/dist/js/bootstrap.bundle.min.js"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
 
</body>
</html>

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>
      </div>
      
      <div class="row">
        <div class="col-sm-12">
          <app-datalist (calCount)="calCount($event)" [data]="data2" [showCount]="true"></app-datalist>
        </div>
      </div>
      
      
      
      <div class="row float-right">
        <div class="col-sm-12 count-text">
          Total Data Count ::  <strong>
            {{totalCount}}
          </strong>
        </div>
      </div>
</div>

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';

@Component({
  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 {
    this.calCount.emit(this.data.length);
  }
}

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>
              {{item.name}}
            </div>
            <span *ngIf="showCount" class="badge bg-primary rounded-pill">14</span>
          </li>
        </ol>
      </div>
    </div>
  </div>

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.

Jassa

Thanks

therichpost
the authortherichpost
Hello to all. Welcome to therichpost.com. 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.