0

Am passing data through routing but unable to retruieve it

From another component (source of the passed data)

this._router.navigate(["/tsafety/checklist-checks", truckdetails:this.regForm.value}]);

Now in the component having the checklist-checks conmponent i have (where am sending the data)

constructor(){

this.sub = this.route.params
  .subscribe(params => {
    this.truckdetails = params["truckdetails"];
    console.log(params["truckdetails"]);
  })
}

The above console displays

[object Object]

How do i read the above parameter data when its displaying an object in the console

UPDATE ROUTES FILE

 {path:'checklist-checks', component:TruckYardChecklistComponent},
6
  • Can you please check console.log(JSON.stringify(params["truckdetails"])); and let me know response Commented Aug 4, 2017 at 7:08
  • this displays "[object Object]" Commented Aug 4, 2017 at 7:12
  • please add your routes path to here Commented Aug 4, 2017 at 7:14
  • Please check the example from here plnkr.co/edit/UjUlWKpO0wxQfB3P6YUG?p=preview Commented Aug 4, 2017 at 7:16
  • I have updated the question with the full details on how am passing the data Commented Aug 4, 2017 at 7:19

4 Answers 4

1

The result of the this.regForm.value is a json object which contains the defined fields, and their respective values.

The result of the console.log is correct because the value assigned to 'truckdetails' in the route params is the .toString() value of the this.regForm.value object.

If you'd like to pass all the data of the form, pass the whole object as optional params like so

this._router.navigate(["/tsafety/checklist-checks", this.regForm.value]);
Sign up to request clarification or add additional context in comments.

2 Comments

Thats true its an object, after passing it that way am still not able to acces some objects contained in the original data, Example a field like material is also an object ( {status:2, label:'new goods ' } ) hence alsoi throws the same problem
In your case you either have to strip down the this.regForm.value into a custom object, which does not have values which are an object, and pass it as the params object. Event better, use a service to communicate the data between one component to another (either by caching the data to a variable on a service, using Observables etc). The way you're using optional parameters to transfer the form data is not correct.
1

You can use below statement to write object to console using javascript.

console.log(JSON.stringify(params["truckdetails"]));

This will print json format string in browser console when you inspect.

6 Comments

this outputs "[object Object]"
it should print json string by converting your object to json format.
are you able to expand your object browser console ? it has a little expando-toggly button beside it that lets you inspect the contents of the object.
May i know reason to down vote my answer? anyone who down vote please also give reason for that.
Am still not able to expand the object browser console, ive upated the question with more details.
|
1

Actually there is small correction in your code and I think it will work:

this._router.navigate(["/tsafety/checklist-checks", this.regForm.value]);

Then in your router file do something like this

{path:'checklist-checks/:truckdetails', component:TruckYardChecklistComponent},

And now your code should work if you are getting value in this.regForm.value

Comments

1

I think you have missed out to passed the params in route path

Replace your one line code

{path:'checklist-checks', component:TruckYardChecklistComponent},

By following line

{ path: 'checklist-checks/:truckdetails', component: ComponentTwo } 

Please read more about route params to here.

Demo

Hope this will help you !!

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.