简体   繁体   English

如何创建不使用路由的AngularJS SPA应用程序?

[英]How can I create an AngularJS SPA application that does not use routing?

I would like to create an AngularJS application that's uses just the one HTTP domain with nothing after the .com. 我想创建一个仅使用一个HTTP域,.com之后不包含任何内容的AngularJS应用程序。 For example an application where this is the only URL that displays in the browser: 例如,其中这是浏览器中唯一显示的URL的应用程序:

www.myapp.com

Is this possible and what would I need to do this? 这可能吗,我需要做什么? All of the applications I have seen so far use something like: 到目前为止,我所看到的所有应用程序都使用类似以下内容的东西:

www.myapp.com/users
www.myapp.com/screen1

etc. 等等

Sure; 当然; all you need is some kind of top-level application state and UI that responds to that state. 您所需要的只是某种顶级应用程序状态和响应该状态的UI。 That's all ngRoute is, under the hood--the state just lives in your address bar, and the configuration for how to respond to that state lives in $routeProvider (which is, in turn, realized in the DOM via the ngView directive). 这就是ngRoute的全部ngRoute -状态仅位于您的地址栏中,而如何响应该状态的配置位于$routeProvider (这又通过ngView指令在DOM中实现)。

Consider this simple example: 考虑以下简单示例:

<div ng-controller="ApplicationController">
  <h1>Some Pages</h1>
  <ul>
    <li><a href='' ng-click='setPage("page1")'>Page 1</a></li>
    <li><a href='' ng-click='setPage("page2")'>Page 2</a></li>
    <li><a href='' ng-click='setPage("page3")'>Page 3</a></li>
  </ul>

  <div ng-include="page + '.html'"></div>
</div>
app.controller('ApplicationController', function($scope) {
  $scope.page = 'page1';

  $scope.setPage = function(pg) {
    $scope.page = pg;
  }
});

You can see this working in a Plunker here: http://plnkr.co/edit/hpnGtMkV6V4JtPRrQmes?p=preview 您可以在下面的Plunker中看到它的工作原理: http ://plnkr.co/edit/hpnGtMkV6V4JtPRrQmes?p=preview

Click the full screen button so you can see the address bar (and the fact that it doesn't change). 单击全屏按钮,以便您可以看到地址栏(以及它不变的事实)。


It is worth nothing that, without storing your application state in the address bar, users don't have a very easy way of bookmarking their place in your app or going forward/back using their browser's buttons. 如果不将应用程序状态存储在地址栏中,用户就没有一种非常简单的方法来标记自己在应用程序中的位置或使用浏览器的按钮前进/后退,这毫无价值。 Carefully consider whether you're building an app that would benefit from this kind of limitation. 仔细考虑您是否正在构建可从这种限制中受益的应用程序。

The routing is typically implemented to make different parts of your app more "bookmarkable". 路由通常是为了使应用程序的不同部分更具“可标记性”而实现的。

So although you may have a SPA, you may still want the user's to use the URL bar to let them get back to that particular part of the application. 因此,尽管您可能拥有SPA,但是您仍然可能希望用户使用URL栏使他们回到应用程序的特定部分。

If you just make your starting page index.html, it should just work without specifying the page name. 如果仅将起始页设置为index.html,则无需指定页面名称即可使用。

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

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