3

I want to pass an id with url using Router in Angular. And i have to fetch that value on another page. Like I have a student list. On click of edit button belongs to particular student on the list. The id of that student pass to edit student details page. After fetching that studentId I want show existing details in the input fields.

So How i can do this?

this is my example path

 { path: 'school-students-list', component: studentsListPageComponent },
  { path: 'edit-student-details', component: studentEdit },
3
  • school-students-list/:id?? Commented Nov 9, 2017 at 5:45
  • @JaydipJadhav yes . id should be passed like this in url. example:( school-students-list/:123) Commented Nov 9, 2017 at 5:47
  • @JaydipJadhav : how i can do this? and how to do this? Commented Nov 9, 2017 at 5:48

2 Answers 2

6

Give the route a parameter like this:

{ path: 'edit-student-details/:id', component: studentEdit }

And then use ActivatedRoute in that component to access the route parameters. Import it from @angular/router

import { ActivatedRoute, Params } from '@angular/router';
import { Subscription } from 'rxjs/Subscription';

Inject this into the constructor

private routeSub: Subscription;

constructor(private route: ActivatedRoute) {}

On init, subscribe to route parameters

ngOnInit(): void {
    this.routeSub = this.route.params.subscribe((params: Params): void => {
        const id = params['id'];
    });
}

Always a good idea to unsubscribe on destroy

ngOnDestroy(): void {
    this.routeSub.unsubscribe();
}

And to link to this page, passing in their member ID, do this on your student listings page:

<a [routerLink]="['/edit-student-details', memberId]">Edit</a>

To navigate via a component method, inject Router from @angular/router and then when you want to navigate, use this.router.navigate(['/edit-student-details', memberId]);

Sign up to request clarification or add additional context in comments.

8 Comments

I updated my answer with how to use ActivatedRoute
in my case the id is dynamic.. that is coming from api response and i assigned it in (memberId). so i can pass member id here or what?
When you navigate to let's say /edit-student-details/1 the id then accessed through ActivatedRoute. Is that what you are trying to do? It's a bit unclear.
i'm having a student list page there i can view list of students (each student is assigned with a unique memberId) this is the response from the API and i'm displaying it. Student List contains (studentlist.html ,and studentlist.ts) and on UI there is a edit button associated with each row .so on click of each raw it should show me the details of the student in text fields for editing . edit page contains (editStudent.ts and html file)
when click event occurs the id should be passed through url and i should be able to fetch that id in that page.. can u help me with this @Jared
|
2

Import ActivatedRoute from @angular/router

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

Access parameter from constructor:

constructor(private route: ActivatedRoute) {
    let id = +this.route.snapshot.params['id'];
    // use the id here
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.