首页 > 解决方案 > 如何解析作为路由Angular 1中查询参数的编码url?

问题描述

在我的路线中,我有这个。但是,我找不到正确解码 url 的方法。该页面只是重定向到基本 url(而不是重定向 url)。url 是这样编码/%2F,什么不是,但我不知道如何制作它,所以我可以接受它作为查询参数。

.config(['$stateProvider', '$urlRouterProvider', function ($stateProvider, $urlRouterProvider) {
   $stateProvider
   .state('example', {
     url: '/{test}/{queryParameterUrl}',
     controller: 'exampleCtrl as example',
     template: require('./partials/test.html')
   });

标签: angularjsangular-ui-router

解决方案


在你的exampleController你可以访问 state 参数,解析它并在以后使用它来重定向,如下所示:

app.controller('exampleCtrl', ['$state', function ($state) {
    var exampleCtrl = this;

    exampleCtrl.parsedRedirectUrl = $state.params.urlredirect.replace(/%2F/g, '/')
}]);

例如,在 HTML 中,使用链接重定向

<a href="{{example.parsedRedirectUrl}}">Redirect</a>

PS在您的状态定义中,使用controllerAs

.config(['$stateProvider', '$urlRouterProvider', function ($stateProvider, $urlRouterProvider) {
    $stateProvider
        .state('example', {
         url: '/{test}/{urlredirect}',
         controller: 'exampleCtrl',
         controllerAs: 'example',
         template: require('./partials/test.html')
    });

推荐阅读