【问题标题】:Serve static html file in angular app在 Angular 应用程序中提供静态 html 文件
【发布时间】:2015-01-17 16:28:03
【问题描述】:

我有一个 Angular 应用程序,因此路由可能如下所示:

angular.module('app').config(function($routeProvider, $locationProvider) {
  $routeProvider

  .when('/', {
    templateUrl : 'views/home.html',
    controller: 'homeController'
  })
  .when('/foo', {
    templateUrl : 'views/foo.html',
    controller: 'fooController'
  })
  .otherwise({
    redirectTo : '/'
  });
  $locationProvider.html5Mode(true);
});

现在,这可能是一个过于简单的问题,但是如果不使用 .when 路由指定它,我可以提供一个永远不会改变并且不需要我添加 javascript 的静态页面吗?例如,假设我想像这样提供 Google 的网站管理员工具验证:

/googlee23dc3443279f430.html

我真的需要创建一个 .when('/googlee23dc3443279f430.html') 路由吗?

编辑:我们还进行了服务器重写,以使非“/”路由仍然提供 index.html 文件,如本 wiki 中所指定(并让 html5mode(true) 在页面刷新时工作):

https://github.com/angular-ui/ui-router/wiki/Frequently-Asked-Questions#how-to-configure-your-server-to-work-with-html5mode

每次我们想要添加静态页面时不必添加重写条件会很好

【问题讨论】:

  • 为什么需要路由?如果静态 html 页面存在于磁盘上,只需将浏览器指向该静态页面的 URL。 Angular 与它无关
  • @link64 好吧,我们正在使用 $locationProvider.html5Mode(true) 并且为了使其与页面刷新一起使用,我们进行了服务器重写以在路由不是时提供应用程序' /',在此 wiki 中指定:github.com/angular-ui/ui-router/wiki/…

标签: angularjs routing


【解决方案1】:

如果整个页面应该用静态 html 替换,您可以使用带有 ng-href 的链接(使其动态和基于数据而不是硬编码)-https://docs.angularjs.org/api/ng/directive/ngHref

【讨论】:

    猜你喜欢
    • 2016-11-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-09-11
    • 2016-12-23
    • 2016-10-26
    • 2017-04-17
    • 1970-01-01
    相关资源
    最近更新 更多