是否可以从angular.js的url中移除#符号?

我仍然希望能够使用浏览器的后退按钮等,当我改变视图,将更新的URL与参数,但我不想要#符号。

教程routeProvider的声明如下:

angular.module('phonecat', []).
  config(['$routeProvider', function($routeProvider) {
  $routeProvider.
  when('/phones', {templateUrl: 'partials/phone-list.html',   controller: PhoneListCtrl}).
  when('/phones/:phoneId', {templateUrl: 'partials/phone-detail.html', controller: PhoneDetailCtrl}).
  otherwise({redirectTo: '/phones'});
}]);

我可以编辑这个有相同的功能没有#吗?


当前回答

第一步:将$locationProvider服务注入到应用配置的构造函数中

步骤2:将代码行$locationProvider.html5Mode(true)添加到应用配置的构造函数中。

第三步:在container (landing, master, or layout)页面中,在标签内添加html标签如<base href="/">。

步骤4:从所有锚标记中删除所有路由配置的“#”。例如,href="#home"变成href="home";Href ="#about"变成herf="about";Href ="#contact"变成Href ="contact"

 <ul class="nav navbar-nav">
     <li><a href="home">Home</a></li>
     <li><a href="about">About us</a></li>
     <li><a href="contact">Contact us</a></li>
</ul>

其他回答

如果你在。net堆栈中使用MVC和AngularJS,这是你必须做的从url中删除'#':

Set up your base href in your _Layout page: <head> <base href="/"> </head> Then, add following in your angular app config : $locationProvider.html5Mode(true) Above will remove '#' from url but page refresh won't work e.g. if you are in "yoursite.com/about" page refreash will give you a 404. This is because MVC does not know about angular routing and by MVC pattern it will look for a MVC page for 'about' which does not exists in MVC routing path. Workaround for this is to send all MVC page request to a single MVC view and you can do that by adding a route that catches all

url:

routes.MapRoute(
    name: "App",
    url: "{*url}",
    defaults: new {
        controller = "Home", action = "Index"
    }
);

为了移除一个漂亮的URL的散列标签,也为了让你的代码在缩小后工作,你需要 像下面的例子一样构建你的代码:

jobApp.config(['$routeProvider','$locationProvider',
    function($routeProvider, $locationProvider) {
        $routeProvider.
            when('/', {
                templateUrl: 'views/job-list.html',
                controller: 'JobListController'
            }).
            when('/menus', {
                templateUrl: 'views/job-list.html',
                controller: 'JobListController'
            }).
            when('/menus/:id', {
                templateUrl: 'views/job-detail.html',
                controller: 'JobDetailController'
            });

         //you can include a fallback by  including .otherwise({
          //redirectTo: '/jobs'
        //});


        //check browser support
        if(window.history && window.history.pushState){
            //$locationProvider.html5Mode(true); will cause an error $location in HTML5 mode requires a  tag to be present! Unless you set baseUrl tag after head tag like so: <head> <base href="/">

         // to know more about setting base URL visit: https://docs.angularjs.org/error/$location/nobase

         // if you don't wish to set base URL then use this
         $locationProvider.html5Mode({
                 enabled: true,
                 requireBase: false
          });
        }
    }]);

你可以调整html5mode,但这只适用于页面的html锚中包含的链接,以及url在浏览器地址栏中的样子。试图从页面外的任何地方请求不带标签(带或不带html5mode)的子页面将导致404错误。例如,以下CURL请求将导致一个page not found错误,与html5mode无关:

$ curl http://foo.bar/phones

虽然下面会返回根/主页:

$ curl http://foo.bar/#/phones

这样做的原因是,在请求到达服务器之前,标签之后的任何内容都会被剥离。因此,对http://foo.bar/#/portfolio的请求作为对http://foo.bar的请求到达服务器。服务器将响应http://foo.bar的200 OK响应(假设),代理/客户端将处理其余的内容。

因此,如果你想与他人分享一个url,你别无选择,只能包含这个标签。

是的,你应该配置$locationProvider并设置html5Mode为true:

angular.module('phonecat', []).
  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);

  }]);

以下两步: 1. 首先在你的应用配置文件中设置$locationProvider.html5Mode(true)。例如: 角。module('test', ['ui.router']) .config(function($stateProvider, $urlRouterProvider, $locationProvider) {$locationProvider. html5mode (true);美元urlRouterProvider.otherwise(“/”);});

2.第二,在主页中设置<base>。 对于eg -> <基地href = " / " >

对于不支持HTML5 History API的浏览器,$location服务将自动回退到散列部分方法。