1

I have a primeng Data table and I want to pass selected row to other page through routers. Ex: I have a student data table and when I select one student row and click on submit button, It should display the details in StudentDetails Page.

Here I can able to select the row and able to pass the data to student component. But unable to pass data to student details component.

export class Student {

selectedRow:Student;// I can able to assign selected row here

navigateStudentDetail(){

this.router.navigate(['./studentdetail']);

}

I can able to pass studentId using query param, But I want to pass my student object to studentdetails component. But unable to do it.

Is there any easy way to achieve this? Please help me

Suraj Rao
  • 29,388
  • 11
  • 94
  • 103
stackUser44
  • 409
  • 4
  • 14
  • 25
  • Using providers I resolved this issue. http://stackoverflow.com/questions/35478994/angular-2-passing-object-via-route-params-possible link helped me a lot. Thanks – stackUser44 Apr 04 '17 at 09:46

1 Answers1

1

Perhaps it's overkill to use a service just to pass some paramenters to another component. You can use NavigationExtras to add custom data along with the Url that you want to navigate to.

import { NavigationExtras} from '@angular/router';
//You will need navigations extras from Router.

somewhere in your component..

      let dataNav:NavigationExtras={queryParams:"somekey":"someValue",
"someOtherKey":"someOtherValue"}};
  this.router.navigate(['/path/whatever/'],dataNav);

then in the target component..

import { Router, ActivatedRoute, Params } from '@angular/router';
//you will need something like this

instantiate ActivatedRoute in your constructor like this

constructor(
private route: ActivatedRoute,
private router: Router){}

finally get the params sent by the first component..

  ngOnInit() {
this.route.queryParams
.subscribe(params =>{
  this.someOtherKeyReceived=params['someOtherKey']
});

}

uajov6
  • 413
  • 7
  • 13