• Home
  • Categories
  • Video Tutorials
    • Angular 5
  • News
  • About us
  • Contact us
  • Login
test
Code4Developers

Code4Developers

Code4Developers
  • Home
  • Categories
  • Video Tutorials
    • Angular 5
  • News
  • About us
  • Contact us
  • Login
  • AngularJs

How To Remove Hash From Application URL In AngularJS

  • Arif Khoja
  • June 10, 2017
  • 3 minute read
angularjs
Total
0
Shares
0
0
0

Introduction

Angular is a framework, which is developed and maintained by Google. It has features such as Two-Way binding, Dependency Injection, Testing and Directives. It is widely used to create a Single Page Application(SPA) with Two-Way binding.

To find more, refer the URL https://docs.angularjs.org/guide

Going further, I assume that you are aware of how to use Angular routing, if not refer Angular routing tutorial.

Why does URL show Hash prefix?

If you are using Angular v1.5 or lesser version, the app URL will show /#/ and from v1.6; it changes to /!#/. When you are using AngularJS routing, it rewrites the URL and shows # in the URL, so by default URL’s are shown below.

  • http://localhost:51287/#/Home
  • http://localhost:51287/#/AboutUs
  • http://localhost:51287/#/ContactUs

Where the path that maps to the folder is removed and # is placed. It is very easy to remove # from the URL.

Only these two things are to be added to make it work.

  • Configuration if $locationProvider.
  • Setting base path for relative location of pages.

$location Service

The $location Service parses the URL in the Browser address bar, which is based on the window.location and makes the URL available to your Application.

Changes to the the URL in the address bar are reflected into $location service and changes to $location are reflected into the Browser address bar.

I would highly recommend reading through the official Angular $location docs before moving forward.

Base Tag

Base tag specifies a default URL for all the relative URL’s in the Application. There can be only one <base> tag in a document and it is written in <head> section of an HTML. It is written, as shown below:

<base href = “” />

Configuring $locationProvider

Let’s start by configuring $locationprovider.

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

    $routeProvider.  

    when('/Home', {  

        templateUrl: ApplicationURL + '/AngularJs/Views/Home.html'  

    }).when('/AboutUs', {  

        templateUrl: ApplicationURL + '/AngularJs/Views/AboutUs.html'  

    }).when('/ContactUs', {  

        templateUrl: ApplicationURL + '/AngularJs/Views/ContactUs.html'  

    }).otherwise({  

        redirectTo: '/Home'  

    });  

    // for enabling html5 mod  

    $locationProvider.html5Mode(true);  

}]); // This is just a sample script. Paste your real code (javascript or HTML) here.

In the code given above, we have configured html5mode.

What is html5mode? It is a standardized way for the manipulation of the Browser URL, so it helps an Angular to change the URL without refreshing the page.

If you are using newer version of an Angular, then you can directly write

$locationProvider.html5Mode(true);

to enable html5mode.

Enabling Base URL (Relative URLs)

To set the base URL in an Application, we must write <base> tag in the head section of an HTML. If the HTML pages, which are to be viewed are in the root path, then we do not have to mention the relative path to the pages and we can simply write <base href=”/”> to map the pages.

If the pages are in different folder like the example, then we should provide the relative path to the folder, as shown below.

<base href="/Demo/Index/">

This will set the view folder to demo/index and render the pages from this path.

Layout.cshtml

<!DOCTYPE html>  

<html>

<head>  

    <meta charset="utf-8" />  

    <meta name="viewport" content="width=device-width" />  

    <title>Routing Demo</title>  

    <script src="~/Scripts/angular.js"></script>  

    <script src="~/Scripts/angular-route.js"></script>  

    <script type="text/javascript">  

        var ApplicationURL = '@WebConfigurationManager.AppSettings["ApplicationURL"]';  

    </script>  

    <script src="~/AngularJS/Modules/DemoModule.js">  

    <!--provide base url for mapping routing in the angular routing-->  

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

<body ng-app="DemoApplicationModule"> <a href="./#/Home">Home</a> 

<a href="./#/AboutUs">AboutUs</a> <a href="./#/ContactUs">ContactUs</a> @RenderBody() </body> 

</html>

 

DemoModule.js

// declared module.  

var DemoApplicationModule = angular.module("DemoApplicationModule", ['ngRoute']);  

//for routing of the pages  

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

    $routeProvider.  

    when('/Home', {  

        templateUrl: ApplicationURL + '/AngularJs/Views/Home.html'  

    }).when('/AboutUs', {  

        templateUrl: ApplicationURL + '/AngularJs/Views/AboutUs.html'  

    }).when('/ContactUs', {  

        templateUrl: ApplicationURL + '/AngularJs/Views/ContactUs.html'  

    }).otherwise({  

        redirectTo: '/Home'  

    });  

    // for enabling html5 mod  

    $locationProvider.html5Mode(true);  

}]);

Conclusion

This is the simplest way to remove Hash (#) from the URL and get a pretty URL.

Arif Khoja
Arif Khoja

Arif Khoja is a Developer. He is a Javascript Enthusiatic who loves logical programming and has first hand experience in building a cutting edge internet product using Angular. He is also an open source freak and keen about learning and sharing. He writes Javascript both frontend and backend. He loves learning and sharing tech all the time. He also has a hands on experience in SEO and writes articles about latest emerging technologies.

Views: 7,034

Share this:

  • Click to share on Facebook (Opens in new window) Facebook
  • Click to share on WhatsApp (Opens in new window) WhatsApp
  • Click to share on LinkedIn (Opens in new window) LinkedIn
  • Click to share on X (Opens in new window) X
  • Click to share on Pinterest (Opens in new window) Pinterest
  • Click to email a link to a friend (Opens in new window) Email
  • Click to print (Opens in new window) Print

Like this:

Like Loading...

Related Posts

Total
0
Shares
Share 0
Tweet 0
Pin it 0
2 comments
  1. Idris says:
    July 29, 2017 at 12:47 pm

    Can you plaese explain.. why i need to provide var ApplicationURL = ‘@WebConfigurationManager.AppSettings[“ApplicationURL”]’;

    Reply
    1. Arif Khoja says:
      August 1, 2017 at 10:51 am

      Hello Idris, actually the demo that i created was in c# so to pass localhost url from webconfig to javascript above code is written.

      Reply

Leave a ReplyCancel reply

Subscribe to Website via Email

Enter your email address to subscribe to this blog and receive notifications of new posts by email.

Recent Posts
  • Getting Started with AWS Step Functions: Orchestration Made Simple

    Getting Started with AWS Step Functions: Orchestration Made Simple

    2 months ago
  • React Hooks Guide: Top Tips for Optimizing Performance in Your React Applications

    React Hooks Guide: Top Tips for Optimizing Performance in Your React Applications

    2 years ago
  • Demystifying JavaScript Tree Shaking: Boosting Performance and Reducing Bundle Size

    Demystifying JavaScript Tree Shaking: Boosting Performance and Reducing Bundle Size

    2 years ago
  • Unlocking the Power of React Hooks: A Comprehensive Guide with Examples

    Unlocking the Power of React Hooks: A Comprehensive Guide with Examples

    2 years ago
  • Celebrating a Decade of Phenomenal Growth: Insights and Reflections on 10 Years of Software Engineering

    Celebrating a Decade of Phenomenal Growth: Insights and Reflections on 10 Years of Software Engineering

    2 years ago
  • Angular Custom Elements: Creating Reusable Components with Angular

    Angular Custom Elements: Creating Reusable Components with Angular

    2 years ago
  • Connect Firebase Realtime NoSQL Database with Angular App from Scratch

    Connect Firebase Realtime NoSQL Database with Angular App from Scratch

    5 years ago
  • How to Build an Inclusive Esports Community

    How to Build an Inclusive Esports Community

    5 years ago
  • Best Digital Icebreakers

    Best Digital Icebreakers

    5 years ago
  • Email alerts when a docker container stopped in AWS ECS CLUSTER

    Email alerts when a docker container stopped in AWS ECS CLUSTER

    5 years ago
Subscribe to Website via Email

Enter your email address to subscribe to this website and receive notifications of new posts by email.

Featured Posts
  • javascript 1
    Spread syntax (three dots) in JavaScript
    • March 21, 2018
  • Angular 2
    Angular 6 CRUD – Part 1: Project Setup, Routing, Service
    • May 9, 2018
  • javascript 3
    Local Storage and Session Storage
    • May 22, 2017
  • Angular 4
    Angular 4 Project Structure
    • June 18, 2017
  • AWS 5
    Email alerts when a docker container stopped in AWS ECS CLUSTER
    • July 24, 2020
Code4Developers
Learning is never ending process

Input your search keywords and press Enter.

%d