Pass object as parameter in $state.go
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
$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; };})();
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});