12

I have an angularJS application that is a gallery. For each picture, associated with it is an ng-href with #/{{files.id}}.

<a ng-href="#/{{files.id}}"...

However, when I click it, the URL that is ultimately displayed is

http://localhost:3000/gallery#%2F0

which destroys my angular routing of

when('/gallery/:imageID', { templateUrl: 'load_image.html' }).

Can someone explain to me how to encode the URL's correctly? Or just use something that doesn't encode the forward slash?

5
  • 4
    Have you tried without the #? Commented Jul 18, 2014 at 1:13
  • 1
    In my case, I had to remove $locationProvider.html5Mode(true); Commented Sep 10, 2014 at 1:19
  • @LarissaLeite Removing the html5More(true) will will write your urls with a # or #/ - simply using / instead of #/ fixed my issue. Commented Oct 25, 2015 at 3:43
  • Possible duplicate of How to generate url encoded anchor links with AngularJS? Commented Dec 24, 2015 at 17:15
  • 2
    Possible duplicate of URL hash-bang (#!/) prefix instead of simple hash (#/) Commented Mar 29, 2017 at 6:47

3 Answers 3

13

Due to aa077e8, the default hash-prefix used for $location hash-bang URLs has changed from the empty string ('') to the bang ('!').

You should try this.

index.html

<li class="active"><a href="#/">Home</a></li>
<li><a href="#/about">About</a></li>

app.js

angular.module('appRoutes',['ngRoute'])
.config(function($routeProvider, $locationProvider){   

    $locationProvider.hashPrefix('');

    $routeProvider
        .when('/',{
            templateUrl:'app/views/pages/home.html'
        })
        .when('/about',{
            templateUrl:'app/views/pages/about.html'
        });
 });
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks! This works fine. The OP should have accepted this answer.
6

There are two solutions:

Not recommended: Add ! in your anchors:

the default hash bang has been changed from "" to "!" in angular and since it will not likely change you could simply make a habit of adding "#!" in your anchor tags instead of "#".

Recommended: Use $locationProvider:

Use

$location.hashPrefix("")

in your module config to use empty string as a hash bang just like old times !!

1 Comment

Adding !# is deprecated now isn't it? I just use standard html /my-url without any hash or bangs and it works with 1.6.4 (html5 mode enabled)
2

Yo need not to encode anything here. Just add * in your path Param as mentioned below and enable html5Mode

  app.config(function ($routeProvider) {
     $routeProvider
    .when('/home', {templateUrl: 'home.html', controller: 'HomeCtrl'})
    .when('/base/:path*', {templateUrl: 'path.html', controller: 'pathCtrl'})
    .otherwise({redirectTo: '/home'});
 });

 $locationProvider.html5Mode({
      enabled: true,
      requireBase: false
    });

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.