簡體   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