20

Is it possible to develop an Angular.js application in a way that would be abstracted from the the web directory path in which it will be deployed?

I am trying to put an Angular.js app in a web server subdirectory http://example.com/myproject/, but the router redirects me to the web server root -- http://example.com.

Below is my Angular.js app:

var myproject = angular.module('myproject', []);

myproject.config(function($routeProvider, $locationProvider) {
  $routeProvider.
    when('/', {templateUrl: 'partials/index.html', controller: IndexCtrl}).
    otherwise({redirectTo: '/'});

  $locationProvider.html5Mode(true);
});



function IndexCtrl($scope, $location) {

}
1
  • 1
    plus one for the user name :) Commented Oct 21, 2015 at 7:52

1 Answer 1

25

Try setting a <base href="/sudirectory"/> in your <head></head>. That's what I needed to do to get mine working, IIRC

Word of caution: This will mess with any anchor tags that have href="#", as well as act as the root for image srcs and the like.

Sign up to request clarification or add additional context in comments.

2 Comments

Another tip that could potentially help: don't forget to use relative paths instead of absolute for the router partial template resources, after setting <base>.
Works for relative paths, but I have AJAX calls that are specified as 'getSomething/'which would work except I needed to set them as '/getSomething/' since it would otherwise get appended to the URL which may have other routing information in it. I want the AJAX calls to resolve to 'mydomain/getSomething/' but if my Angular router has me at 'mydomain/article/1/' it gets appended. I had no choice but to create a global base_url variable and prepend it to all my AJAX URLs. Would be nice to just set this as a config within Angular, but luckily the project is small enough to just do by hand.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.