🎓 Top 15 Udemy Courses (80-90% Discount): My Udemy Courses - Ramesh Fadatare — All my Udemy courses are real-time and project oriented courses.
▶️ Subscribe to My YouTube Channel (178K+ subscribers): Java Guides on YouTube
▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube
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']);
}
}
My Top and Bestseller Udemy Courses. The sale is going on with a 70 - 80% discount. The discount coupon has been added to each course below:
Build REST APIs with Spring Boot 4, Spring Security 7, and JWT
🆕 High-Demand
80–90% OFF
[NEW] Learn Apache Maven with IntelliJ IDEA and Java 25
🆕 High-Demand
80–90% OFF
ChatGPT + Generative AI + Prompt Engineering for Beginners
🚀 Trending Now
80–90% OFF
Spring 7 and Spring Boot 4 for Beginners (Includes 8 Projects)
🔥 Bestseller
80–90% OFF
Available in Udemy for Business
Available in Udemy for Business
Building Real-Time REST APIs with Spring Boot - Blog App
🔥 Bestseller
80–90% OFF
Available in Udemy for Business
Available in Udemy for Business
Building Microservices with Spring Boot and Spring Cloud
🌟 Top Rated
80–90% OFF
Available in Udemy for Business
Available in Udemy for Business
Java Full-Stack Developer Course with Spring Boot and React JS
🔥 Bestseller
80–90% OFF
Available in Udemy for Business
Available in Udemy for Business
Build 5 Spring Boot Projects with Java: Line-by-Line Coding
🌟 Top Rated
80–90% OFF
Testing Spring Boot Application with JUnit and Mockito
🔥 Bestseller
80–90% OFF
Available in Udemy for Business
Available in Udemy for Business
Spring Boot Thymeleaf Real-Time Web Application - Blog App
🔥 Bestseller
80–90% OFF
Available in Udemy for Business
Available in Udemy for Business
Master Spring Data JPA with Hibernate
🔥 Bestseller
80–90% OFF
Available in Udemy for Business
Available in Udemy for Business
Spring Boot + Apache Kafka Course - The Practical Guide
🎓 Student Favorite
80–90% OFF
Available in Udemy for Business
Available in Udemy for Business
Comments
Post a Comment
Leave Comment