Ludwig Magnusson
Ludwig Magnusson

Reputation: 14399

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.

Example: 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 [hostname]/mycontext/newaccount.

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.

Upvotes: 4

Views: 8306

Answers (1)

Valentyn Shybanov
Valentyn Shybanov

Reputation: 19391

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:

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

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

Upvotes: 4

Related Questions