【问题标题】:angular routeProvider recognize '%2F' but not '/' in url角度 routeProvider 识别 '%2F' 但不是 '/' 在 url
【发布时间】:2017-01-24 04:34:43
【问题描述】:

给定以下路线:

        when('/contactus', {
            templateUrl: 'pages/contactus.html'
        }).
        when('/unsubscribe/:email', {
            templateUrl: 'pages/unsubscribe.html'
        }).
        when('/users/validate?key', {
            templateUrl: 'pages/uservalidation.html'
        })

在我启动这个应用程序之后

npm start

在浏览器地址栏中,如果我输入:

localhost/contactus <- GET /contactus 200
localhost/unsubscribe/abc <- GET /unsubscribe/abc 404
localhost/unsubscribe%2Fabc <- GET /unsubscribe%2Fabc 200
localhost/users/validate?key=1 <- GET /users/validate?key=1 404
localhost/users%2Fvalidate?key=1 <- GET /users%2Fvalidate?key=1 404
localhost/users%2Fvalidate%3Fkey%3D1 <- GET /users%2Fvalidate%3Fkey%3D1 404 

所以我的问题是: 1)如何使 /users/validate?key=1 工作? 2)这是一种不使用url编码的方法吗?通过编码,浏览器将 /unsubscribe%2Fabc 转换为预期的 /unsubscribe/abc,但刷新页面或共付并粘贴此 url 将无法再次使用,因为它再次使用 '/' 而不是编码值。

【问题讨论】:

  • 这可能对你没用,但我建议你使用ui-router 这个库扩展了ngRouter,并在查询中拥有你想要的参数类型。还有嵌套视图
  • 假设你设置了$locationProvider.html5Mode,你是否在服务器端配置了URL重写?
  • @Phil 你能更具体一点吗?我假设你在谈论 base bref="/"
  • read this

标签: angularjs url-routing ngroute


【解决方案1】:

让其他人遇到这个问题:

我所做的解决方案是按照 Phil 在评论中提到的那样更改服务器。

我的服务器端是节点,它有:

app.get('/:page', callback);

我所做的只是将其更改为:

app.get('/*', callback);

这个解决方案解决了我的两个问题。

【讨论】:

    【解决方案2】:
     app.config([
    '$locationProvider', '$routeProvider',
    function ($locationProvider, $routeProvider) {
        $locationProvider.html5Mode({
            enabled: true,
            requireBase: false
        }).hashPrefix('!');
        var viewBase = '/app/views/';
        $routeProvider         
       .when('/admin/home/appliedlist/:userid1/:userid2/:userid3', { // For appliedlist page 
            templateUrl: viewBase + 'EditAppliedList.html',
            controller: 'EditController'
        })
        .otherwise({   // This is when any route not matched => error
            templateUrl: viewBase + 'Error.html'           
        })
    }]);
    

    获取url数据

       app.controller('EditController', ['$scope',  '$routeParams', function   ($scope, $routeParams) {    
        $scope.Type = $routeParams.userid3;
         $scope.Name = $routeParams.userid2;
         $scope.ID = $routeParams.userid1;
    }]);
    

    并将数据传递给 url

     <a href="/admin/home/appliedlist/12/pravin/employee">Edit</a>
    

    你也可以使用like对数据进行编码和解码

      <a href="/admin/home/appliedlist/12/pravin/{{MyencodingLogic(employee)}}">Edit</a>
    

    将编码代码放入函数'MyencodingLogic'

    和解码代码在 EditController 中使用,您可以通过 $routeParams 获得价值

    【讨论】:

    • 我猜你在说我的问题1?我希望 url 使用查询字符串,例如 validate?key=1&identifier=2 这与您的代码提供的路由参数不同。
    猜你喜欢
    • 2014-04-03
    • 1970-01-01
    • 1970-01-01
    • 2014-08-01
    • 1970-01-01
    • 2019-03-29
    • 1970-01-01
    • 1970-01-01
    • 2017-10-10
    相关资源
    最近更新 更多