Ludwig Magnusson Ludwig Magnusson - 11 months ago 51
AngularJS Question

Is it possible to change the base url in angular?

I am writing a web app with a java servlet back end and I am checking out angular for the front end. Since the address to a specific resource within a servlet depends on what servlet context it is placed in, I am wondering if it is possible to change the base url for all angular functions.

If I create a link in my webapp

<a href='/newaccount'>Create new account</a>
and I configure the route provider accordingly
$routeProvider.when('/newaccount', ...
I want this to work no matter what servlet context my servlet is placed in. So if I place my servlet in a context called "mycontext" the actual url to the resource will be

Can I somehow setup angular and pass in the context name so that it is prepended to all locations and configured routes? This way the servlet could be placed in different contexts and still work.

Answer Source

Actually there are two routing - one on backend and one on frontend. Aren't you missing them?

When you're configuring $routeProvider in AngularJS, you're configuring client-side routing, so urls would be kind of [hostname]/anypath/yourpage/#/newaccount (if you're using HashBang mode, that is default in AngularJS).

But of course you can change base. Plunker, for example use this way:

      document.write('<base href="' + document.location + '" />');

More information, including HTML5 and HashBang mode you can find here:$location (there is also note about <base> and Html link rewriting)