在哪里捕获 run 语句中的 $location.search() 变量?为了重建国家 [英] Where to capture the $location.search() vars in the run statement? In order to rebuild the state

查看:29
本文介绍了在哪里捕获 run 语句中的 $location.search() 变量?为了重建国家的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果用户直接转到这样的链接,则重建应用的状态:http://localhost:8080/app/#!/tag_id=5&ticker=GOOG

Rebuild the state of the app if the user directly goes to a link like this: http://localhost:8080/app/#!/tag_id=5&ticker=GOOG

应用目前仍然通过 $urlRouterProvider.otherwise('/login');

在我的应用程序中,我使用 ngCookies$state (ui-router) 变量存储在 cookie 中.

In my app I am storing the $state (ui-router) variables in a cookie using ngCookies.

$rootScope.$on('$stateChangeSuccess', 
function(event, toState, toParams, fromState, fromParams) {
    const shareUrl = () => {
        // const base = 'https://www.tickertags.com/dashboard/#/';
        const base = 'http://localhost:8080/app/#!/';
        const cookie = $cookies.get('stateCookie');
        console.log('state vars', $httpParamSerializer(JSON.parse(cookie)));
        const parsedState = $httpParamSerializer(JSON.parse(cookie));
        return `${base}${parsedState}`;
    };
    // console.log('state.current.name', $state.current.name)
    const stateCookie = $cookies.get('stateCookie');
    $cookies.putObject('stateCookie', $state.params);
    console.log('shareUrl', shareUrl());
})

改变状态时:

日志 'state var' 将记录:state vars tag_id=5&ticker=GOOG

并且日志 'shareUrl' 将记录:http://localhost:8080/app/#!/tag_id=5&ticker=GOOG

我想先打印该链接的 $location.search() 结果.下面我在 $stateChangeStart eventListener 中放了一个新日志.

I'm trying to just print the $location.search() results of that link first. Below I put a new log in the $stateChangeStart eventListener.

.run(['$rootScope', '$location', '$state', '$cookies', '$httpParamSerializer',
function($rootScope, $location, $state, $cookies, $httpParamSerializer) {

    $rootScope.$on("$stateChangeError", console.log.bind(console));
    $rootScope.$on('$stateChangeStart',
    function(event, toState, toParams, fromState, fromParams, options) {
        const searchObject = $location.search();
        console.log('searchObject', searchObject);
    });

结果

我被重定向回登录,这是现在预期的.但是我的 searchObject 还是空的?

Results

I got redirected back to the login, which is expected right now. However my searchObject is still empty?

注意我还在 .run 的第一行放置了一个断点.调试器停在那里,我尝试在 URL 正确时注销 $location.search(),但它仍然打印出一个空对象...

Note I also put a break point on the first line inside of the .run. The debugger stopped there and I tried to log out $location.search() while the URL was correct, but it still printed out an empty object...

当我刚刚注销 $location 时,我确实看到了所有这些:

When I just log out $location however I do see all of this:

在哪里放置 const searchObject = $location.search(); 行,以便我可以正确捕获粘贴的 URL,然后创建新参数来运行 $state.go 重建状态?

Where to I placed that const searchObject = $location.search(); line so I can correctly capture the pasted URL, then created new params to run a $state.go on to rebuild the state?

推荐答案

刚刚想通了!

我需要在仪表盘后添加?:

I needed to add ? after dashboard:

const base = 'http://localhost:8080/app/#!/dashboard?';

现在它可以工作了:

这篇关于在哪里捕获 run 语句中的 $location.search() 变量?为了重建国家的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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