Mr world wide
Mr world wide

Reputation: 4814

want to remove # from my angularjs application

want to remove # from my angularjs application i have tried $locationProvider, but there is no luck

here is my Config :

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"
    })
}]);

i am calling a URL using ui-sref:

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

The URL in browser appear like this :

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

Upvotes: 0

Views: 78

Answers (2)

d-bro82
d-bro82

Reputation: 500

It is called hashbang. To remove it you have to enable html5-mode in your angular config like this:

    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);
    }]);

You can read more about hashbang and html-mode here: https://docs.angularjs.org/guide/$location

Upvotes: 1

user6885115
user6885115

Reputation:

Have you tried the following

$locationProvider and html5Mode

Do this when defining your Angular application and configuring your routes.

$locationProvider.html5Mode(true);

Set relative links

Set a <base> in the <head> of your document.

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

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

Change .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]

Upvotes: 0

Related Questions