想从我的angularjs应用程序

时间:2016-12-07 15:15:10

标签: angularjs angular-ui-router hashbang ui-sref location-provider

想从我的angularjs应用程序中删除# 我试过$ locationProvider,但没有运气

这是我的配置:

var TechdefeatApp = angular.module('TechdefeatApp',['ui.router']);
TechdefeatApp.config(['$stateProvider', '$urlRouterProvider', function($stateProvider, $urlRouterProvider){
    $urlRouterProvider.otherwise("/")

    $stateProvider
    .state('/', {
        url: "/",
        templateUrl: "app/components/home/homeView.html",
        controller:"homeController"
    })
    .state('post', {
        url: "/post/:seo_url",
        templateUrl: "app/components/post/postView.html",
        controller:"postController"
    })
}]);

我正在使用ui-sref调用网址:

<a ui-sref="post({seo_url:post.seo_url})" title="{{post.title}}">{{post.title}}</a>

浏览器中的网址如下所示:

URL : http://jaancari.com/latest/#/post/un-ambassador-nikki-haley
URL : http://jaancari.com/latest/#/post/december-3-1984-bhopal-worst-industrial-accident-in-history

2 个答案:

答案 0 :(得分:1)

它被称为hashbang。要删除它,您必须在角度配置中启用html5-mode,如下所示:

    var TechdefeatApp = angular.module('TechdefeatApp',['ui.router']);
    TechdefeatApp.config(['$stateProvider', '$urlRouterProvider', '$locationProvider', function($stateProvider, $urlRouterProvider, $locationProvider ){
        $urlRouterProvider.otherwise("/")

        $stateProvider
        .state('/', {
            url: "/",
            templateUrl: "app/components/home/homeView.html",
            controller:"homeController"
        })
        .state('post', {
            url: "/post/:seo_url",
            templateUrl: "app/components/post/postView.html",
            controller:"postController"
        })

        $locationProvider.html5Mode(true);
    }]);

您可以在此处阅读有关hashbang和html-mode的更多信息:https://docs.angularjs.org/guide/$location

答案 1 :(得分:0)

您是否尝试过以下

$ locationProvider和html5Mode

在定义Angular应用程序和配置路由时执行此操作。

$locationProvider.html5Mode(true);

设置相对链接

在文档的<base>中设置<head>

<!doctype html>
<html>
<head>
    <meta charset="utf-8">

    <base href="/">
</head>

更改.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]