在Angular.js中的子目录中路由

时间:2022-10-13 12:16:34

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?

是否有可能以一种从将部署它的Web目录路径中抽象出来的方式开发Angular.js应用程序?

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.

我试图将Angular.js应用程序放在Web服务器子目录http://example.com/myproject/中,但路由器将我重定向到Web服务器根目录 - http://example.com。

Below is my Angular.js app:

下面是我的Angular.js应用程序:

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


25  

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

尝试在 中设置。这就是我需要做的事情,让我的工作,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.

注意事项:这将使任何具有href =“#”的锚标签混乱,并充当图像srcs之类的根。

#1


25  

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

尝试在 中设置。这就是我需要做的事情,让我的工作,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.

注意事项:这将使任何具有href =“#”的锚标签混乱,并充当图像srcs之类的根。