Routing in subdirectory in Angular.js

sssilver picture sssilver · Oct 12, 2012 · Viewed 14.3k times · Source

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) {

}

Answer

Ben Lesh picture Ben Lesh · Oct 12, 2012

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.