简体   繁体   English

在angularjs中使用html5Mode的问题

[英]Issue with html5Mode in angularjs

I'm trying to remove index.html of the url using html5Mode(true) in angularjs, this is the code: 我正在尝试使用html5Mode(true)删除url的index.html ,这是代码:

angular.module('myApp', [
  'ngRoute',
  'myApp.filters',
  'myApp.services',
  'myApp.directives',
  'myApp.controllers'
]).
config(['$routeProvider', '$locationProvider', function($routeProvider, $locationProvider) {
  $locationProvider.html5Mode(true);
  $routeProvider.when('/home', {templateUrl: 'views/home.html'});
  $routeProvider.when('/about', {templateUrl: 'views/about.html'});
  $routeProvider.otherwise({redirectTo: '/'});
}]);

If I dont write $locationProvider.html5Mode(true); 如果我不写$locationProvider.html5Mode(true); the url shows: 网址显示:

localhost:(port)/MyApplication/index.html 本地主机:(端口)/MyApplication/index.html

If I write $locationProvider.html5Mode(true); 如果我写$locationProvider.html5Mode(true); the url shows: 网址显示:

localhost:(port) 本地主机:(端口)

MyApplication is removed of the url. MyApplication已删除网址。 And I want the url shows: 我想要网址显示:

localhost:(port)/MyApplication/ 本地主机:(端口)/所有MyApplication /

What I'm doing wrong? 我做错了什么? What's the problem? 有什么问题?

UPDATE: 更新:

How should show my <a> tags? 应该如何显示我的<a>标签? Right now I have: 现在我有:

<a href="#/about">About</>

When I click on the link, the url shows: 当我点击链接时,网址显示:

localhost:(port)/MyApplication/index.html#/about 本地主机:(端口)/MyApplication/index.html#/about

I'm lost with this. 我迷失了。

Thanks in advance! 提前致谢!

If your application runs under /MyApplication/ try to set the base path in your index.html and switch on html5Mode : 如果您的应用程序在/MyApplication/下运行,请尝试在index.html中设置基本路径并启用html5Mode

<html>
  <head>
    <base href="/MyApplication/index.html" />
    ...

See Using $location . 请参阅使用$ location

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

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