Passing encoded slashes in URLs can be tricky because the browser tends to change them into regular slashes. One solution that has worked for me is double encoding the slashes. I used a helpful online encoder tool, but you can also achieve this within your Angular app using encodeURIComponent
. This results in the URL looking like
#/http%3A%252F%252Fwww.google.com
.
In order to set up the parameter, you need to add it to the routing configuration in your app.js
file. I added a parameter called url
and marked it as optional using the ?
symbol:
app.config(function ($routeProvider, $locationProvider) {
$routeProvider
.when('/:url?', {
templateUrl: 'view.html',
controller: 'MainCtrl'
})
.otherwise({
redirectTo: '/'
});
});
Next, in your controller, you can utilize $routeParams
to retrieve the URL. Remember to use decodeURIComponent
to decode the double-encoded slashes:
app.controller('MainCtrl', function($scope, $routeParams) {
$scope.url = decodeURIComponent($routeParams.url);
})
Feel free to click on the link provided in the demo to see how the Google URL works.
Check out the Demo