如何角UI路由器传递$ state.go()自定义数据?
问题描述:
我想通过 $ state.go通过自定义对象到另一个状态()
在UI的路由器。
I want to pass a custom object to another state via $state.go()
in UI-Router.
var obj = {
a: 1,
b: 2,
fun: function() {
console.log('fun');
}
}
$state.go('users', obj);
但我需要运行乐趣()
的目标状态,所以我无法传递URL参数此对象。在目标控制器,我试图通过 $ stateParams
来获取 OBJ
的价值,但得到的空对象 {}
:
But I need to run fun()
in target state, so I can't pass this object in URL parameter. In target controller, I tried to fetch the value of obj
via $stateParams
but got empty object {}
:
function UserCtrl($stateParams) {
conrole.log($stateParams); // empty
}
因此,如何 OBJ
传递给国家用户是否正确?
So how to pass obj
to state "users" correctly?
答
像这样的参数定义状态:
Define state with parameters like this:
$stateProvider
.state('user', {
url: '/user',
params: {
obj: null
},
templateUrl: 'templates/user.html',
controller: 'UserCont'
})
这样的调用时传参数:
when calling pass parameter like this:
$state.go('user',{obj: myobj});
在控制器UserCon收到这样的参数:
in the controller UserCon receive parameter like:
$state.params.obj
用户$状态是像
function UserCon($scope, $http, $state){