Angular UI路由器将查询字符串放在哈希之前

angular ui router places query string before hash

本文关键字:哈希之 字符串 查询 UI 路由器 Angular      更新时间:2023-09-26

我有一个使用外部支付提供商购买积分的应用程序。购买成功后,支付提供者将重定向到http://any.url/?orderNr=xxxx&outcome=SUCCESS。这个url是一个使用UI路由器的AngularJS应用。路由器的配置如下:

$stateProvider
    .state('dashboard', {
        url: '/dashboard',
        templateUrl: 'views/dashboard.html',
        controller: 'DashboardCtrl',
        isAuthenticated: true
    })
    .state('login', {
        url: '/login',
        templateUrl: 'views/login.html',
        controller: 'LoginCtrl',
        isAuthenticated: false
        });
$urlRouterProvider.otherwise('/login');

因此,当用户返回到http://any.url的根目录并且已经登录时,它将重定向到仪表板。这是付款后的情况,因为您无法在没有登录的情况下付款。问题是,hashbang被放置在查询字符串之后,所以仪表板的url变成了http://any.url/?orderNr=xxxx&outcome=SUCCESS#/dashboard。在DashboardCtrl中,我无法使用$location.search()获得查询字符串。是否有任何方法来获得查询字符串的"角度的方式",所以不是解析location.href ?

如果你需要#/dashboard路由的查询字符串,你应该能够在url配置中指定参数。

.state('dashboard', {
    url: '/dashboard?orderNr&outcome',

Per the docs