Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

$location hash prefix

Tags:

angularjs

I'm just starting out with Angular, so this might be a common newbie mistake but I am trying to achieve the following url format:

http://myapp.localhost/index.html#!/about

Which I believe should be the default for Angular?

This is my configuration:

angular.module('App', []).config(function($routeProvider, $locationProvider, VIEWS_ROOT) {
    $locationProvider.html5Mode(false);
    $locationProvider.hashPrefix = '!';

    // Routing
    $routeProvider.when('/', {templateUrl: './welcome.html', controller: 'WelcomeController'});
   $routeProvider.when('/about', {templateUrl: './about.html', controller: 'AboutController'});
})
.run(function($rootScope) {
    //...
});

In my html I have a simple anchor like so:

<a href="#!/about">About</a>

However when I click that anchor, the resulting URL constructed is:

http://myapp.localhost/index.html#/!/about

Which obviously fails to match any of my routes... a bit stumped on what's actually happening here, or what I'm doing wrong. I'm running off my local Apache instance under a vhost. There's nothing going on with mod_rewrite - so it looks like Angular is doing this.

like image 537
leepowell Avatar asked Feb 01 '13 08:02

leepowell


People also ask

What is html 5 mode?

In HTML5 mode, the $location service getters and setters interact with the browser URL address through the HTML5 history API. This allows for use of regular URL path and search segments, instead of their hashbang equivalents.

What is $location in AngularJS?

Overview. The $location service parses the URL in the browser address bar (based on the window. location) and makes the URL available to your application. Changes to the URL in the address bar are reflected into $location service and changes to $location are reflected into the browser address bar.

How to update the URL in AngularJS?

If the user is maximizing it the first time, share the url to this maximized view with the maximizedWidgetId on the UI. As long as you use $location(which is just a wrapper over native location object) to update the path it will refresh the view. Not only $location will refresh the view.


1 Answers

It's a method to set the hashPrefix, not a property. $locationProvider.hashPrefix('!');

like image 161
leepowell Avatar answered Dec 12 '22 07:12

leepowell