AngularJS how to alter incoming request URI

Viewed 23

My AngularJS app uses the following URL pattern:

https://myapp/#!/mypage

But I now have to accept one incoming request from another app using this URI:

https://myapp/myotherpage

Note that the /#!/ is missing from the incoming request.

How do I reformat the incoming request URI so that it becomes

https://myapp/#!/myotherpage
2 Answers

You can follow the below code if you want to alter the URI using code

var incomingUrl ="https://myapp/myotherpage";
var uriPart=incomingUrl.split("myapp");
var actualUri=uriPart[0]+"myapp/#!"+uriPart[1]
console.log(actualUri);

You can use actualUri variable as a URL( it will be # converted url)

Not sure, where the request to URL https://myapp/myotherpage is incoming exactly.

If it is coming through until your AngularJS app, then you could modify your router-configuration as shown below.

Otherwise you need URL-rewriting or redirects on your server.

See also: and similar question: AngularJS routing without the hash '#'

On the client-side

In AngularJS you could use HTML5 routing mode by setting

app.config(function($routeProvider,$locationProvider) {

    $routeProvider.when('/myotherpage', {
        templateUrl:'/html/myotherpage.html'
    });

    $locationProvider.html5Mode(true);
})

This will also remove the hash-bang (#!) as your app's internal route-prefix.

See details in documentation for locationProvider.

Related