Angular UI Router删除斜杠?参数

时间:2017-02-17 09:50:38

标签: angularjs angular-ui-router

目前我遇到了这个问题:Angular ui route stateparam in substate

在像{transactionid}[transactionid]这样的状态网址中尝试使用路径参数和其他方法后,我注意到之前有一个/添加了?参数。所以,URL看起来像是:

website.com/page/?transactionid=someid

但是,我认为一定是

website.com/page?transactionid=someid

我们目前使用此代码

删除任何网址的尾部斜杠
$urlMatcherFactoryProvider.strictMode(false);

$urlRouterProvider.rule(function($injector, $location) {
    var path = $location.path();
    var hasTrailingSlash = path[path.length-1] === '/';

    if(hasTrailingSlash) {
        //if last charcter is a slash, return the same url without the slash  
        var newPath = path.substr(0, path.length - 1); 
        return newPath; 
    }
});

但是,如何在'问题'标记之前删除'/'?

“付款成功”的路线(请参阅链接中的问题)目前如下:

.state('shop.payment-success', {
    url: '/payment-success{transactionid}',
    templateUrl: '/views/payment-success.html',
    params: {
        transactionid: {
            value: null,
            squash: true
        }
    },
    resolve: {
        transactionid: ['$stateParams', function($stateParams){
            console.log($stateParams);
            return $stateParams.transactionid
        }]
    }

1 个答案:

答案 0 :(得分:0)

我认为最好使用标准路由(你有更多的选择,它更容易......而且不必删除任何东西)......类似的东西:

.state('shop.payment-success', {
    url: '/payment-success/:value/:squash', //<-- HERE YOU SET THE PARAM .. YOU CAN HAVE MORE THAN ONE
    templateUrl: '/views/payment-success.html',

    resolve: {
        transactionid: ['$stateParams', function($stateParams){
            console.log($stateParams);
            return {value:$stateParams.value,squash:stateParams.squash}
        }]
    }