如何角UI路由器传递$ state.go()自定义数据? [英] How to pass custom data in $state.go() in angular-ui-router?

查看:233
本文介绍了如何角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){

这篇关于如何角UI路由器传递$ state.go()自定义数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆