Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pass object as parameter in $state.go

I want to navigate to another state/screen and pass a simple json object to this next screen.

I have the following:

var benefit = { "x": "y"};
$state.go('pages.claimed', { 'benefit': benefit });

My state looks like this:

.state('pages.claimed', {
  url: '/claimed',
  views: {
    'page': {
      templateUrl: 'templates/pages/claimed.html'
    }
  }
})

I can't however access the "benefit" object/parameter in the pages.claimed view. I'm using the ionic framework based on angular.

like image 203
Jorre Avatar asked Aug 04 '14 09:08

Jorre


People also ask

Can you pass an object to a state variable?

You can pass an object because "State variables can hold objects and arrays just fine, so you can still group related data together". I am just trying to figure out how to update a state variable in case i decide to pass an object to useState rather than defining many state variables as above.

What is the best way to define parameters in a state?

The key point is: Type of defined parameters and toParamas of $state.go should be same array or object on both sides of state transition. For example when you define a params in a state as follows you means params is array because of using " []":

How do I pass a pass parameter to a route?

Pass params to a route by putting them in an object as a second parameter to the navigation.navigate function: navigation.navigate ('RouteName', { /* params go here */ }) Read the params in your screen component: route.params. We recommend that the params you pass are JSON-serializable.

How do you pass an object as an argument in Python?

Passing an Object as argument. To pass an object as an argument we write the object name as the argument while calling the function the same way we do it for other variables. Syntax: function_name(object_name); Example: In this Example there is a class which has an integer variable ‘a’ and a function ‘add’ which takes an object as argument.


Video Answer


3 Answers

Parse object to json:

var benefit = angular.toJson({ "x": "y"});

Define variable in state params:

.state('pages.claimed', {
   url: '/claimed?params',
   views: {
     'page': {
       templateUrl: 'templates/pages/claimed.html'
     }
   }
})

Access to variable from controller via $stateParams:

var benefit = angular.fromJson($stateParams.benefit);

Here full doc

Edit:

There are several ways to pass an object to controller from url:

Via query params:

define options url: '/yoururl?a&b&c',

pass variables yoururl?a=1&b=2&c=3

Via url params:

define options url: '/yoururl/:a/:b/:c',

pass variables yoururl/1/2/3

For more complicated situations you can parse your object to json string and encode it with base64

Object: { a:1, b:2, c:3 } JSON String: {"a":1,"b":2,"c":3} Base64 Encoded string: eyJhIjoxLCJiIjoyLCJjIjozfQ==

define options url: '/yoururl?params'

pass variables yoururl?params=eyJhIjoxLCJiIjoyLCJjIjozfQ==

More info about base64

like image 185
Umidbek Karimov Avatar answered Oct 17 '22 22:10

Umidbek Karimov


$state.go should be corrected like this

var benefit = { "x": "y"};
$state.go('pages.claimed', { benefit: benefit });

.state should be like this

.state('pages.claimed', {
  url: '/claimed',
  params: { benefit: null },
  views: {
    'page': {
      templateUrl: 'templates/pages/claimed.html'
    }
  }
})

Catch the passed object as follows in the next controller

(function () {
    'use strict';

    angular.module('YourAppName').controller('ControllerName', ['$stateParams', ControllerName]);

    function ControllerName($stateParams) {
		var vm = this;
        vm.variableToBind = $stateParams.benefit;
    };
})();
like image 28
Chinthaka Suren Fernando Avatar answered Oct 17 '22 22:10

Chinthaka Suren Fernando


Very clean solution:

app.js:

$stateProvider.state('users', {
    url: '/users',
    controller: 'UsersCtrl',
    params: { obj: null }
})

controllers.js

function UserCtrl($stateParams) {
    console.log($stateParams);
}

Then from any other controller:

$state.go('users', {obj:yourObj});
like image 24
askilondz Avatar answered Oct 17 '22 22:10

askilondz