0

Hope someone can advise the best way to achieve what I want.

I have a user grid where you can click edit it then goes to the manage user form.

What I want to do is pass the selected user id over to the manage user form and display this user, how best would I go about achieving this?

Thanks Andy

1 Answer 1

1

In your grid.component.html, when user clicks on a grid edit button, call a function and pass the corresponding grid data id as shown below.

<button (click)="goToManageForm(gridId)"> Edit</button>

In your grid.component.ts, pass the id as a param to the manageForm component.

private goToManageForm(id:any){
   this.router.navigate([`/manageForm/${id}`]);
}

In your routing component, add a route to manageForm component which expects a parameter.

const routes: Routes = [

{
    path: '',
    component: MyMainComponent,
    canActivate: [AuthGuard],
    children: [
      { path: 'grid',    component: GridComponent },
      { path: 'manageForm/:gridId',    component: ManageFormComponent },
]
}

You can access the passed grid id in the manageForm.component.ts as shown below.

import { Component, OnInit } from '@angular/core';
import { Router,ActivatedRoute } from '@angular/router';
import { Subscription } from 'rxjs';

export class manageFormComponent {

   private subscription:Subscription;
   private gridId:any; 

   constructor(private activatedRoute:ActivatedRoute){}

   ngOnInit(): void {

      // subscribe to router event
      this.subscription = this.activatedRoute.params.subscribe(
        (param: any) => {
           if(typeof param['gridId'] !== 'undefined' && param['gridId'].trim !='' ){ 
              this.gridId = param['gridId'];
           }
      });
   }
 }

Now the passed gridId will be accessible in this.gridId

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

5 Comments

Hi Veena, thanks for the reply, I will get it a go and let you know how I get on
@stephenad pls feel free to ask me if you are stuck with any errors
Hi Veena thanks for your reply I have got this working so greatly appreciated, I do have a related follow up question should I open a new ticket for that?
I do have one issue I had to comment out the canActivate: [AuthGuard], line what do I need to get that to be accepted and work do I need a specific import?
@stephenad no, you do dont need any specific import for that. Pls go through this link angular.io/api/router/CanActivate . If you still have issues, pls open a ticket regarding the issue. Thanks :)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.