如何在$ state.go函数中同时将参数传递给父状态和子状态?

时间:2018-07-16 18:05:35

标签: javascript angularjs redirect routing angular-ui-router

我想将用户重定向到特定状态。我有2个州。

第一个是app,URL是/{lang},例如mydomain.com/en

app的子状态为app.login。此状态的URL为/login/{slug}。因此,包括app状态在内的完整链接可能是mydomain.com/en/login/settings

下一个状态调用login,URL为/login/settings,例如。 mydomain.com/login/settings。当用户输入此链接时,我想将他重定向到app.login状态,并为settings传递{slug}参数。但是我还必须为{lang}状态传递更早的app参数。

我如何分别为appapp.login传递两个参数,以便重定向用户。

它应该看起来像这样:$state.go('app.login', { lang: 'en', redirect: 'settings')。第一个参数应该用于app状态,第二个参数应该用于app.login状态。但是我找不到合适的方法。

.state('app', {
    abstract: true,
    url: '/{lang}', // Example url: /en
    params: {
            lang: null
    }
})
.state('app.login', {
    abstract: true,
    url: '/login/{slug}', // Example url: /en/login/settings
    params: {
            redirect: null
    }
})
.state('login', {

    // When user enters '/login/settings', should be redirected to the 'app.login' state
    url: '/login/settings',
    controller: ['$state', function($state) {
            // So, I have to pass two parameters - 'lang' and 'redirect'
            // But first parameter is for to the 'app' state and the second to the 'app.login' state.
            // How can I pass a parameter at the same time for a parent and a child in one '$state.go'?
            $state.go('app.login', { lang: 'en', redirect: 'settings' })

            // Full link should looks like `/en/login/settings`
            // === `/{lang}/login/{slug}`
    }
})

0 个答案:

没有答案
相关问题