简体   繁体   中英

angularjs remove index.html

UPDATE

AngularJS works now. Switched to WebStorm IDE, but also had problem setting it up, that helped me: Bower "Git not in the PATH" error (Bower “Git not in the PATH” error).

Soon I will try ngrouting under WebStorm again.

How can I remove index.html from my localhost URL? There are similar questions, but they couldn't help me.

Here is what I did so far:

index.html:

<head><title>MyApp</title></head>    
<body>
  <div ng-view>
      <a href="#/menu">Menu</a>
      <a href="#/main">Main</a>
  </div>
</body>

app.js:

var app = angular.module('myApp', [
  'ngRoute',
  'ngResource',
  'HTML5ModeURLs'
});

config.js:

app.config(['$routeProvider', '$locationProvider',
function ($routeProvider, $locationProvider) {
    $route.html5Mode(true);

    $routeProvider.
        when('/menu', {
            templateUrl: 'Modules/Menu/view.html',
            controller: 'Modules/Menu/controller'
        }).
        when('/main', {
            templateUrl: 'Modules/Main/view.html',
            controller: 'Modules/Main/controller'
        }).
        otherwise({
            redirectTo: '/menu'
        });
}
]);

You try to remove index.html from your app url ?

app.config(['$routeProvider', '$locationProvider',
function ($routeProvider, $locationProvider) {
$route.html5Mode(true);

$routeProvider.
     when('/', {
        templateUrl: 'index.html',
        controller: 'HomeCtrl'
    }).
    when('/menu', {
        templateUrl: 'Modules/Menu/view.html',
        controller: 'Modules/Menu/controller'
    }).
    when('/main', {
        templateUrl: 'Modules/Main/view.html',
        controller: 'Modules/Main/controller'
    }).
    otherwise({
        redirectTo: '/menu'
    });
  }
]);

Setting the route '/' should do the job I guess . That's how I do it anyway.

You are using IIS Express to host your AngularJS application files, so I think this topic was what you were looking for. Yagiz Ozturk solution suggests you to configure your IIS server this way to rewrite URL, so you will be able to skip the index.html part :

<system.webServer>
<rewrite>
  <rules>
    <rule name="AngularJS" stopProcessing="true">
      <match url=".*" />
      <conditions logicalGrouping="MatchAll">
        <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
        <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
        <add input="{REQUEST_URI}" pattern="^/(api)" negate="true" />
      </conditions>
      <action type="Rewrite" url="/" />
    </rule>
  </rules>
</rewrite>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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