【问题标题】:Issue with html5Mode in angularjsangularjs中的html5Mode问题
【发布时间】:2013-11-06 04:36:42
【问题描述】:

我正在尝试在 angularjs 中使用html5Mode(true) 删除网址的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: '/'});
}]);

如果我不写$locationProvider.html5Mode(true); url 显示:

localhost:(端口)/MyApplication/index.html

如果我写$locationProvider.html5Mode(true);,网址显示:

本地主机:(端口)

MyApplication 已从 url 中删除。 我想要网址显示:

localhost:(端口)/MyApplication/

我做错了什么?有什么问题?

更新:

应该如何显示我的<a> 标签?现在我有:

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

当我点击链接时,网址显示:

localhost:(端口)/MyApplication/index.html#/about

我迷路了。

提前致谢!

【问题讨论】:

  • 请提供您定义路线的代码。 (你调用 $routeProvider.when())
  • @bekite 我已经更新了帖子,抱歉耽搁了。问候。

标签: html url angularjs location-provider


【解决方案1】:

如果您的应用程序在/MyApplication/ 下运行,请尝试在您的 index.html 中设置基本路径并打开html5Mode

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

Using $location

【讨论】:

  • 我期待所有像 'dir/file.jpg' 这样的 href 都被翻译成 '/MyApplication/index.html/dir/file.jpg' 但令我惊讶的是浏览器正在执行 '/MyApplication /dir/file.jpg'
  • @Reto Aebersold 当你刷新网页时?问题还在这里。使用此模式需要在服务器端重写 URL,基本上您必须重写所有指向应用程序入口点的链接
  • @biology.info 是的,这就是您在 SPA 中必须做的事情
猜你喜欢
  • 2016-01-18
  • 1970-01-01
  • 2015-08-22
  • 1970-01-01
  • 2015-08-21
  • 2016-03-06
  • 2013-08-22
  • 2015-02-25
  • 2015-02-16
相关资源
最近更新 更多