简体   繁体   English

如何使用Angularjs在URL内部没有#的单页应用程序?

[英]How to have single page application without # inside the URL using Angularjs?

I am trying to build an single page application with angular js. 我正在尝试使用角度js构建单页面应用程序。 But i don't want # inside the url when the template is included. 但是,当包含模板时,我不希望在URL内部使用 I have used the 我用过了

 $locationProvider.html5Mode(true) 

app.config(['$routeProvider', '$locationProvider',
  function ($routeProvider, $locationProvider) {
        $routeProvider.
      when('/dashboardnew', {
                templateUrl: './dashboard_tmp',
                controller: 'CategoryCtrl'
            }).
      otherwise({
                redirectTo: '/dashboardnew'
            });
        // use the HTML5 History API
        $locationProvider.html5Mode(true);
    }]);

but this didn't work when the page is reloaded. 但重新加载页面时这不起作用。 How should I solve this problem? 我该如何解决这个问题? Thanks 谢谢

You cannot, that's the compromise. 你不能,这是妥协。 The html5 mode only works if angular is loaded, which is why it will not work if the page is reloaded or if paths are bookmarked. html5模式仅在加载角度时才有效,这就是为什么如果重新加载页面或者路径被加入书签,它将无法工作。 Stick with the #, people are starting to get used to seeing them 坚持#,人们开始习惯看到它们

ofcourse you can, use html5 mode in angular and rewrite every (non-api) request to your angular home page on server side. 当然,你可以在角度使用html5模式,并将每个(非api)请求重写到服务器端的角度主页。 angular will pick up url and go to url location angular将获取网址并转到网址

You need to edit your .htaccess file 您需要编辑.htaccess文件

RewriteEngine on

# Don't rewrite files or directories
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]

# Rewrite everything else to index.html to allow html5 state links
RewriteRule ^ index.php [L]

RewriteRule: index.php or index.html RewriteRule:index.php或index.html

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

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