web-dev-qa-db-ja.com

AngularJS:URLから#!/(bangプレフィックス)を削除する方法?

私はすでに$locationProvider.html5Mode(true);を実行しましたが、機能していません。 _http://example.com_にアクセスすると、常に_http://example.com/#!/_にアクセスします。コードはここにあります:

_var myApp = angular.module('myApp', ['ui.router', 'ui.bootstrap']);

myApp.config(['$qProvider', function ($qProvider) {
    $qProvider.errorOnUnhandledRejections(false);
}]);

myApp.config(function($stateProvider, $urlRouterProvider,$locationProvider) {
    // For any unmatched url, redirect to EXTRANET home page
    $urlRouterProvider.otherwise('/');
    // Now set up the states
    $stateProvider
    .state('listrole', {
    url: "/list-role",
    views: {
      'main-content': {
        templateUrl: rootUrl+ 'views/main.html',
        controller: 'rolesCtrl'
      }
    }
    })
    $locationProvider.hashPrefix('');
    $locationProvider.html5Mode(true);
});
_

pdate$locationProvider.hashPrefix('');も追加しましたが、違いはありません。また、アドレスバーにアドレスを入力してEnterキーを押していることもお伝えします。私は正しくやっていますか?ブラウザはサーバーから更新する必要がないことをどのように見つけますか?

アップデート#2

理想的には、URLを_http://example.com_および_http://example.com/#!/list-role_から_http://example.com/list-role_にしたい。現在_http://example.com/list-role_は404を与えます

アップデート#

それが役立つ場合は、nginxプロキシを使用しています。

アップデート#4

消去されたキャッシュ。 _http://example.com_の代わりに_http://example.com/#!_が表示されますが、それでも_http://example.com/list-role_は404を示します

9
Volatil3

このプレフィックスを削除する場合は、このコードを構成に追加します。

appModule.config(['$locationProvider', function($locationProvider) {
  $locationProvider.hashPrefix('');
}]);

ここにソース 詳細情報。


更新

接頭辞全体(#だけでなく!)も削除したい場合は、 この解決策 を試してみてください。

1)HTML5モードをアクティブにして、モジュールconfigのプレフィックス!を削除します

$locationProvider.html5Mode(true);
$locationProvider.hashPrefix('');

2)次に、index.htmlの/でbaseを<head>に設定します

<head>
    ...
    <base href="/">
</head>
20
Mistalis

以下をapp.jsに追加します

app.config(['$locationProvider', function($locationProvider) {

  $locationProvider.hashPrefix('');

}]);
0
Pranav V R

以下のコードは、URL内の感嘆符(!)マークを削除します

$locationProvider.hashPrefix('');

または、 yeoman setup を使用したAngularJSプロジェクトのルーティングの問題に進みます

それは私のために働いた

0
Shiva

HTMLファイルに追加_<head> <base href="/foldername/"> </head>_

そしてこれはあなたのapp.js用です$locationProvider.html5Mode(true);

これは、あなたが持っていない_.htaccess_作成ファイル用です。

_RewriteEngine On 
# If an existing asset or directory is requested go to it as it is
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} -f [OR]
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} -d  
RewriteRule ^ - [L]
RewriteRule ^ /foldername/
_
0
Myco Claro

AngularJSを使用したMVCで.NETスタックを使用している場合、URLから「#」を削除するには、次のようにする必要があります。

1. _Layoutページでbase hrefを設定します。

 <head> <base href="/"> </head>

2.次に、angular app configに以下を追加します:

$locationProvider.html5Mode(true)
$locationProvider.hashPrefix("!");
0