gpt4 book ai didi

javascript - 在哪里捕获运行语句中的 $location.search() 变量?为了重建国家

转载 作者:行者123 更新时间:2023-12-03 04:40:18 24 4
gpt4 key购买 nike

目标/预期

如果用户直接访问如下链接,则重建应用程序的状态:http://localhost:8080/app/#!/tag_id=5&ticker=GOOG

结果

应用目前仍通过 $urlRouterProvider.otherwise('/login'); 重定向回 /login

<小时/>

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

$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 中放置了一个新日志。

.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 仍然是空的?

enter image description here

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

enter image description here

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

enter image description here

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

最佳答案

刚刚想通了!

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

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

现在它可以工作了:

enter image description here

关于javascript - 在哪里捕获运行语句中的 $location.search() 变量?为了重建国家,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43121888/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com