内部问号或angularjs的url有问题

Having problems with question marks inside or the url with angularjs

本文关键字:url 有问题 angularjs 内部      更新时间:2023-09-26

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

我要么想重写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>

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

如何使用AngularJS生成url编码的锚链接?