📲How To Share Data Between Components Using Subscription And Subject In Angular 6

As we know that there were following ways to share the data between components.

  1. Parent to Child: Sharing Data via Input
  2. Child to Parent: Sharing Data via ViewChild
  3. Child to Parent: Sharing Data via Output() and EventEmitter

Apart from the above approach, we can use the sharing service to pass the data between components.

There was a scenario where we can show the number of records based on the search data. There were two separate components named header and product was created to achieve the problem.

Solution

I had used an ngx-spinner and message service to achieve this functionality. If you look closely at the application you can see that there is a search bar in the top header which is used to search the data and the record is displayed based on the search record. You can set auto-search functionality in this case. Because when we start the search, it will automatically populate the data which you want to search and based on the data you need to apply to the spinner functionality as it might take few seconds to load the data if it is a huge number of record.

I have created a sample POC to illustrate the use of shared service using subject and subscription to pass the data between components.

Following things are used to develop this POC.

  1. Angular 6,
  2. Bootstrap 4
  3. Add the required packages like ngx-spinner, primeng.

I would suggest you download the sample code and explore for more information.

Create a Header component and design accordingly.

header.component.ts

  1. import { Component, OnInit } from '@angular/core';
  2. import { Subscription } from 'rxjs';
  3. import { MessageServiceService } from '../services/message-service.service';
  4. @Component({
  5. selector: 'app-header',
  6. templateUrl: './header.component.html',
  7. styleUrls: ['./header.component.css']
  8. })
  9. export class HeaderComponent implements OnInit {
  10. searchText:any;
  11. constructor(private message_service: MessageServiceService) {
  12. }
  13. ngOnInit() {
  14. }
  15. onSearch() {
  16. this.message_service.sendProductId(this.searchText);
  17. }
  18. }

Create product component and design accordingly

product.component.ts

  1. import { Component, OnInit } from '@angular/core';
  2. import { Subscription } from 'rxjs';
  3. import { MessageServiceService } from '../../services/message-service.service';
  4. import { UserService } from '../../services/user.service';
  5. import { NgxSpinnerService } from 'ngx-spinner';
  6. @Component({
  7. selector: 'app-product',
  8. templateUrl: './product.component.html',
  9. styleUrls: ['./product.component.css']
  10. })
  11. export class ProductComponent implements OnInit {
  12. subscription: Subscription;
  13. productName: string;
  14. productList: any;
  15. constructor(private message_service: MessageServiceService, private productService: UserService,private spinner: NgxSpinnerService) {
  16. this.subscription = this.message_service.getProductID().subscribe(message => {
  17. this.productName = message.text;
  18. if (this.productName) {
  19. this.getProduct(this.productName);
  20. }
  21. });
  22. }
  23. ngOnInit() {
  24. this.productList = this.productService.getProductData();
  25. }
  26. getProduct(name) {
  27. this.spinner.show();
  28. setTimeout(() => {
  29. /** spinner ends after 5 seconds */
  30. this.spinner.hide();
  31. this.productList=this.productService.getProductDataByName(name);
  32. }, 5000);
  33. //this.productList=this.productService.getProductDataByName(name);
  34. // this.spinner.hide();
  35. }
  36. }

Create shared service

message-service.service.ts

  1. import { Injectable } from '@angular/core';
  2. import { Subject } from 'rxjs';
  3. import { Observable } from 'rxjs';
  4. @Injectable()
  5. export class MessageServiceService {
  6. private subject = new Subject<any>();
  7. constructor() { }
  8. sendProductID(message: string) {
  9. this.subject.next({ text: message });
  10. }
  11. getProductID(): Observable<any> {
  12. return this.subject.asObservable();
  13. }}

The output of the application will look like below,

Screen 1

When you run the application you will get all the data showing on the screen.
Share Data Between Components Using Subscription And Subject In Angular

Screen 2

Enter the product what you want to search and click on search button.
Share Data Between Components Using Subscription And Subject In Angular

Screen 3

Share Data Between Components Using Subscription And Subject In Angular

Up Next
    Ebook Download
    View all
    Learn
    View all
    sourabhsomani.com