Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AngularJS converting my ng-href url "slash" into "%2f"

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?

like image 811
ForgetfulFellow Avatar asked Jul 17 '14 22:07

ForgetfulFellow


3 Answers

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'
        });
 });
like image 74
Abhishek Kulshrestha Avatar answered Nov 19 '22 18:11

Abhishek Kulshrestha


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 !!

like image 27
socket_var Avatar answered Nov 19 '22 17:11

socket_var


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
    });
like image 2
Ankur Gupta Avatar answered Nov 19 '22 18:11

Ankur Gupta