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.
- product-list.component.html
- product-list.component.ts
- product-list.component.css
- 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:
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
- Communication from parent to child (Pass Parent variable in child )
- 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.
- @Input
- @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
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;
}
}
<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:
I hope it will clear the topic of reusable components in angular. Download or clone the project