【问题标题】:Having problems with question marks inside or the url with angularjs内部问号或 angularjs 的 url 有问题
【发布时间】:2016-02-10 19:00:45
【问题描述】:

我在允许用户查看帖子名称中包含问号或多个问号的帖子时遇到了一些问题。当访问 url mysite.com/posts/user/postname 时,angular 会获取 :user 和 :postName 并检索必要的数据,但如果 :postName 包含一个或多个问号,它似乎会中断路由并且页面可以'找不到帖子,因为它与带有问号的实际帖子名称不匹配。但是,如果我输入带有问号 %3f 的 ascii 版本的帖子名,它可以工作并显示正确的数据。

我要么想重写 url 来替换?使用 %3f 或修复我的角度脚本。路由如下所示,控制器的相关部分如下所示。

.when('/posts/:postUser/:postName', {
                title: 'View Post',
                templateUrl: 'posts/view-post.php',
                controller: 'userSingleCtrl',
                resolve: {
                    post: function(services, $route) {
                        var postName = $route.current.params.postName;
                        var postUser = $route.current.params.postUser;
                        return services.getUserSingle(postName, postUser);
                        return services.getComments(postName, postUser);
                    }
                }
            })

控制器:

var postName = ($routeParams.postName) ? $routeParams.postName : 'Doesnt Exist';
    var postUser = ($routeParams.postUser) ? $routeParams.postUser : 'Doesnt Exist';
    var original = post.data;
    original._name = postName;
    original._user = postUser;
    $scope.post = angular.copy(original);
    $scope.post._name = postName;
    $scope.post._user = postUser;

以防万一任何当前的 htaccess 规则导致这种情况,而不是真正的角度,我的 htaccess 也是如此。

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/?(.*)$ /#/$1 [L,QSA]

 <IfModule mod_expires.c> 
   ExpiresActive off 
ExpiresByType text/html "access 1 month" 
ExpiresByType text/css "access 1 month" 
ExpiresByType application/javascript "access 1 month" 
ExpiresByType text/javascript "access 1 month" 
   ExpiresByType text/plain "access 1 month" 
   ExpiresByType image/jpg "access 1 month" 
   ExpiresByType image/jpeg "access 1 month" 
   ExpiresByType image/gif "access 1 month" 
   ExpiresByType image/png "access 1 month" 
   ExpiresByType application/pdf "access 1 month" 
   ExpiresByType application/x-shockwave-flash "access 1 month" 
   ExpiresDefault "access 2 days" 
  </IfModule> 

<IfModule mod_deflate.c>
<FilesMatch "\.(html|php|txt|xml|js|css)$">
SetOutputFilter DEFLATE
</FilesMatch>
</IfModule>

【问题讨论】:

    标签: javascript angularjs .htaccess url mod-rewrite


    【解决方案1】:

    以下链接显示了使用角度过滤器应用 uri 编码的示例。您可能会做同样的事情来生成您的帖子链接来编码? (和其他字符)转换为有效的 uri。

    How to generate url encoded anchor links with AngularJS?

    【讨论】:

    • 这太棒了,谢谢,我添加了过滤器并将转义过滤器传递到实际刷新的 ng-href 中,页面加载完美,url 中有 %3F!
    猜你喜欢
    • 2012-10-15
    • 1970-01-01
    • 1970-01-01
    • 2015-05-30
    • 2013-07-01
    • 1970-01-01
    • 1970-01-01
    • 2017-04-29
    • 2014-09-15
    相关资源
    最近更新 更多