繁体   English   中英

Angular JS:如何摆脱url中的app / index.html并停止显示目录内容

[英]Angular JS: How to get rid of app/index.html in the url and stop displaying directory content

我分叉了有角种子项目来尝试angularJS,而且我遇到了一些URL问题。 我如何摆脱app / index.html部分来访问index.html文件,以便localhost:8000 / app / index.html成为localhost:8000? 另外,看起来我定义的路线也被忽略了。

/* app/js/app.js */

var myApp = angular.module('myApp', [
  'ngRoute',
  'myAppControllers'
]);

myApp.config(['$routeProvider', '$locationProvider',
                function($routeProvider, $locationProvider) {
  /* I can't even find how to access these urls */
  $routeProvider.when('/', {
    templateUrl: 'partials/home.html',
    controller: 'HomeCtrl'
  })
  .when('/navigation', {
    templateUrl: 'partials/file-navigation.html',
    controller: 'FileNavigationCtrl'
  })
  .otherwise({
    redirectTo: '/'
  });

  $locationProvider.html5Mode(true);
}]);

/* app/js/controllers.js */

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

myAppControllers.controller('FileNavigationCtrl', function($scope) {
  $scope.files = [ /* some files */ ];
});

myAppControllers.controller('HomeCtrl', function($scope) {
  $scope.user = 'TestUser';
});

例如,如何进入导航页面? 我试过了:

  • 本地主机:8000 /导航
  • 本地主机:8000 / app /导航
  • 本地主机:8000 / app / index.html /导航
  • 本地主机:8000 / app / index.html#/ navigation

它们都不起作用,但是最后一个将URL更改为localhost:8000 /#%2Fnavigation,而其他返回404错误。

要摆脱index.html您需要更改此示例的服务器部分, https://github.com/angular/angular-seed/blob/master/scripts/web-server.js

服务器尝试解析URI,并从磁盘或目录列表中提供文件。 如果请求/则可以添加逻辑来提供index.html ,类似

if (path === './') { process.chdir('app') return self.sendFile_(req, res, 'index.html') }

但是,我认为您不应该在现实生活中使用它。 让自己表达出来 ,然后从那里开始。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM