How to Pass Property from One Component to Another in Angular 8

In this post, I show you how to pass property or field from one component to another in Angular 8.
Let's consider, We have Employee Management App and we want to update or view details of the particular employee then we need to pass employee unique id from one component to another.
In this post, we use Angular ActivatedRoute and Router modules.
  • ActivatedRoute contains route-specific information such as route parameters, global query params etc.
  • Router is used to navigate from one component to another component.
First, we will see how to navigate from one component to another component and then we will see how to pass employee id from one component to another component.

Using Router

It is used to navigate from one component to another component. To use a Router in any component, follow the steps.

Step 1: Import Router

import { Router } from '@angular/router'; 

Step 2: Router Instance

Make Router service available in component using dependency injection with a constructor.
constructor(private router: Router) { 
} 

Step 3: Using a Router navigate() Function

Call navigate() method of Router and pass path and parameter if any, to navigate from one component to another component. Find the code snippet.
this.router.navigate(['/details', id]); 
Here URL /details/:id will be the path to navigate. When the navigate() method will be executed, the component mapped with URL /details/:id will be displayed.
Here is the complete code:
import { EmployeeDetailsComponent } from './../employee-details/employee-details.component';
import { Observable } from "rxjs";
import { EmployeeService } from "./../employee.service";
import { Employee } from "./../employee";
import { Component, OnInit } from "@angular/core";
import { Router } from '@angular/router';

@Component({
  selector: "app-employee-list",
  templateUrl: "./employee-list.component.html",
  styleUrls: ["./employee-list.component.css"]
})
export class EmployeeListComponent implements OnInit {
  employees: Observable<Employee[]>;

  constructor(private employeeService: EmployeeService,
    private router: Router) {}

  ngOnInit() {
    this.reloadData();
  }

  reloadData() {
    this.employees = this.employeeService.getEmployeesList();
  }

  deleteEmployee(id: number) {
    this.employeeService.deleteEmployee(id)
      .subscribe(
        data => {
          console.log(data);
          this.reloadData();
        },
        error => console.log(error));
  }

  employeeDetails(id: number){
    this.router.navigate(['details', id]);
  }
}

ActivatedRoute

To use ActivatedRoute in our component, follow the below steps.

Step 1. Import ActivatedRoute Service

import { ActivatedRoute } from '@angular/router';

Step 2: Inject ActivatedRoute Instance

Make ActivatedRoute available in component using dependency injection with a constructor.
  constructor(private route: ActivatedRoute,private employeeService: EmployeeService) { }

Step 3: Routing with Parameters

Now suppose a URL /details/100 is being accessed. To understand the fetching of a parameter, find the mapping of a component which we configure in the module.
  { path: 'details/:id', component: EmployeeDetailsComponent }
URL /details/100 will invoke EmployeeDetailsComponent . The path parameter will be accessed by id as given in path mapping with a component.
Here is code to fetch id from URL:
  ngOnInit() {
    this.employee = new Employee();

    this.id = this.route.snapshot.params['id'];
    
    this.employeeService.getEmployee(this.id)
      .subscribe(data => {
        console.log(data)
        this.employee = data;
      }, error => console.log(error));
  }
Code snippet to access employee id, which was passed from /details/:id
 this.id = this.route.snapshot.params['id'];
Here is the complete code:
import { EmployeeService } from './../employee.service';
import { Employee } from './../employee';
import { Component, OnInit } from '@angular/core';
import { Router } from '@angular/router';

@Component({
  selector: 'app-create-employee',
  templateUrl: './create-employee.component.html',
  styleUrls: ['./create-employee.component.css']
})
export class CreateEmployeeComponent implements OnInit {

  employee: Employee = new Employee();
  submitted = false;

  constructor(private employeeService: EmployeeService,
    private router: Router) { }

  ngOnInit() {
  }

  newEmployee(): void {
    this.submitted = false;
    this.employee = new Employee();
  }

  save() {
    this.employeeService.createEmployee(this.employee)
      .subscribe(data => console.log(data), error => console.log(error));
    this.employee = new Employee();
    this.gotoList();
  }

  onSubmit() {
    this.submitted = true;
    this.save();    
  }

  gotoList() {
    this.router.navigate(['/employees']);
  }
}

Comments