Tuesday, May 17, 2016

AngularJS : Passing Object as a State Parameter

In this post let’s see how we can pass Object to another state as a state parameter.

When defining the state, you need to specify that the state is expecting a state parameter. Here it's named as “myobjectparameter”.
.state("app.somestate.someotherstate", {
    url: "/someotherstate/:myobjectparameter",
    templateUrl: "sometemplate.tpl.html",
    controller: "SomeTemlateController",
And when you are navigating to “app.somestate.someotherstate” and specifying the value for “myobjectparameter”, you need to convert your object to a JSON string.
$state.go("app.somestate.someotherstate", {
  "myobjectparameter": JSON.stringify({
      "property1": "Property Value1",
      "property2": "Property Value2"
And from “app.somestate.someotherstate” state, you need to  parse "$stateParams.myobjectparameter" which was passed as a string to a JSON object.
var myObjectParameter = JSON.parse($stateParams.myobjectparameter);
Happy Coding.


No comments:

Post a Comment