2016-03-18 49 views
0

我想刪除從我的網址#,所以我有下面的代碼:

angular.module('building') 
    .config(['$stateProvider', '$locationProvider', 
     function ($stateProvider, $locationProvider) { 

      // Turn on #-less links. 
      $locationProvider.html5Mode(true); 

      $stateProvider 
       .state('building', { 
        url:'/building', 
        templateUrl: 'app/modules/building/building.html', 
        controller: 'BuildingQuery', 
        controllerAs: 'vm' 
      }) 

    }]); 

而且我在index.html文件有<base href="/">

而代碼工程...排序。

如果我進入這樣的鏈接...

http://127.0.0.1:4000/#/building 

然後將瀏覽器重定向這個URL ...

http://127.0.0.1:4000/building 

和加載頁面。

但是,如果我進入這個網址...

http://127.0.0.1:4000/building 

然後...

Cannot GET /building 

因爲#網址重定向正確,我知道$locationProvider.html5Mode(true);是做什麼

任何想法?知道我正在使用angm生成模塊可能很有用。

回答

0

從角docs

使用[html5mode]要求URL重寫服務器端的,基本上你 不得不重寫所有的鏈接到你的應用程序 的入口點(如index.html的) 。要求使用<基地的>標記對於此 的情況也很重要,因爲它允許Angular區分作爲應用程序基礎的url部分和應用程序應該處理的路徑 。

I.e.你需要告訴你的服務器重寫/建立到/index.html的任何請求。

+0

當然。明顯。謝謝! – crowhill

0

evsheino指出我在正確的方向。我的代碼沒有問題,這是導致問題的服務器配置。我所要做的就是添加以下到我的gruntfile.js

middleware: function (connect, options) { 
    var optBase = (typeof options.base === 'string') ? [options.base] : options.base; 
        return [require('connect-modrewrite')(['!(\\..+)$/[L]'])].concat(
         optBase.map(function(path){ return connect.static(path); })); 
       }, 
       keepalive: true, 
       port: 4000, 
       base: '.', 
       hostname: 'localhost', 
       debug: true, 
       livereload: true, 
       open: true 
      } 
     } 
    }, 

充分說明可以在這裏找到:http://jjt.io/2013/11/16/angular-html5mode-using-yeoman-generator-angular/

編輯:這是爲connect