在运行语句中的哪里捕获$ location.search()var?为了重建状态 [英] Where to capture the $location.search() vars in the run statement? In order to rebuild the state

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

问题描述

如果用户直接转到以下链接,则重新构建应用程序的状态: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 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?';

现在它将起作用:

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

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