如何从angularjs中的已解析数据中获取ui-router状态参数

时间:2017-03-20 13:56:02

标签: javascript angularjs angular-ui-router

如何从服务中获取状态参数? 我有这个状态:

.state('cashbox.list', {
        title: 'Cashbox',
        url: '/:from/:to?q',
        templateUrl: '/cashbox_list.html',
        controllerUrl: 'controllers/CashboxCtrl.js',
        controller: 'CashboxCtrl',
        controllerAs: 'mainCtrl',
        params: {
            from: getCashboxParamFrom,
            to: getCashboxParamTo
        },
        resolve: {
            cashboxData: getTransactionsData
        }
    })



    function getTransactionsData($stateParams)
    {
        return CashboxService.getTransactions($stateParams.from, $stateParams.to);
    }

现在我的问题是,从和的params 是动态的,并由另一个服务提取。在决定获得参数之前,我需要一些像决心一样的东西。我已经尝试使用 getCashboxParamTo / From 中的promises进行操作,但应用程序在进入状态时会停止路由。还试图将带有 redirectTo 的父状态用于子状态,但没有找到任何关于如何从父状态下的已解析数据动态创建stateparams的信息。

        .state('cashbox', {
            title: trns('title.cashbox_list'),
            url: '/cashbox',
            redirectTo: {
                state: 'cashbox.list',
                params: HERE DATES FROM RESOLVE
            },
            resolve: {
                dates: getFromToDates
            }
        })

使用 state.go 的解决方案无法正常运行,因为我遇到了“转换已被不同转换”问题所取代。

1 个答案:

答案 0 :(得分:0)

答案是宣布两个州 cashbox cashbox.list 。然后在从服务获取数据后重定向 onStart 转换挂钩:

$transitions.onStart({to: 'cashbox'}, function (trans)
    {
        var curMonth = gfService.getCurrentMonth();
        return $state.target('cashbox.list', {from: curMonth.dateFrom.string, to: curMonth.dateTo.string})
    });