简体   繁体   English

AngularJS-使用$ locationProvider删除哈希值

[英]AngularJS - Remove hash # using $locationProvider

I used $locationProvider to remove hashtag # from this url 我使用$locationProvider从此url删除hashtag #

http://localhost/angular-phonecat/app/#/phones

using the following code: 使用以下代码:

phonecatApp.config(['$routeProvider', '$locationProvider', function($routeProvider, $locationProvider) {

  $routeProvider.
    when('/phones', {templateUrl: 'partials/phone-list.html', controller: 'PhoneListCtrl'}).
    when('/phones/:phoneId', {templateUrl: 'partials/phone-detail.html', controller: 'PhoneDetailCtrl'}).
    otherwise({redirectTo: '/phones'});

  $locationProvider.html5Mode(true);

}]);

and in index.html added this in the head 并在index.html中将其添加到头部

<base href="/angular-phonecat/app/">

this worked and my current url is: 这有效,我当前的网址是:

http://localhost/angular-phonecat/app/phones

But when I directly access this url it is not showing the webpage because it is not calling the index.html file. 但是当我直接访问该url它没有显示该网页,因为它没有调用index.html文件。 Instead, it is showing the contents of /app/phones directory 而是显示/app/phones目录的内容

在此处输入图片说明

I tried by creating an .htaccess file also 我也尝试通过创建.htaccess文件

RewriteEngine   On
RewriteBase     /
RewriteCond     %{REQUEST_URI} !^(/index\.php|/img|/js|/css|/robots\.txt|/favicon\.ico)
RewriteCond     %{REQUEST_FILENAME} !-f
RewriteCond     %{REQUEST_FILENAME} !-d
RewriteRule     ./index.html [L]

There is a small error in your rule, the rewrite rule requires one more parameter. 规则中有一个小错误,重写规则需要另一个参数。

Furthermore, when you write RewriteCond %{REQUEST_FILENAME} !-d you prevent redirection if a directory with this name exists. 此外,当您编写RewriteCond %{REQUEST_FILENAME} !-d ,如果存在具有该名称的目录,则可以防止重定向。 Since /anular-phonecat/app/phones exist you need to also remove this line. 由于/anular-phonecat/app/phones存在,因此您还需要删除此行。

You dont need the RewriteCond %{REQUEST_URI} !^(/index\\.php|/img|/js|/css|/robots\\.txt|/favicon\\.ico) condition, since files won't be redirecte thanks to the RewriteCond %{REQUEST_FILENAME} !-f condition 您不需要RewriteCond %{REQUEST_URI} !^(/index\\.php|/img|/js|/css|/robots\\.txt|/favicon\\.ico)条件,因为文件不会通过以下方式重定向RewriteCond %{REQUEST_FILENAME} !-f条件

The result is: 结果是:

RewriteEngine   On
RewriteBase     /angular-phonecat/app/ 
RewriteCond     %{REQUEST_FILENAME} !-f
RewriteRule     .* ./index.html [L]

Edited response based on comments 根据评论编辑回复

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM