Angularjs 路由如何为浏览器外的应用程序工作?

How does Angularjs routing work for applications outside the browser?

我正在使用 AngularJS 创建单页应用程序 (mobile/desktop)。基于我对 AngularJS 的有限了解,我认为 apps/websites 的路由是基于 url 和 $location/$location.path 指令使用的。但是,在移动或桌面应用程序中,没有浏览器。那么,如果需要切换视图,AngularJS 路由在这种情况下如何工作?

谢谢

如果您谈论的是 Angular 应用程序本身,它总是需要一些东西来解释。 Angular 写在 JavaScript 中,这意味着它必须由理解 JavaScript 的东西来解释。我使用 interpreted instead of compiled 这个词,因为 JavaScript 不是编译语言。

但是,您问解释 JavaScript 的东西如何在我的屏幕上显示它?为此,您需要一些背景信息。

DOM

这是我们到达文档对象模型的地方 DOM。来自 W3c:

The Document Object Model is a platform- and language-neutral interface that will allow programs and scripts to dynamically access and update the content, structure and style of documents. The document can be further processed and the results of that processing can be incorporated back into the presented page. This is an overview of DOM-related materials here at W3C and around the web.

将上面的引述变哑:

您有一个正在显示的文档(网页),DOM 允许您更改正在显示的文档。

JavaScript 引擎

JavaScript 和 DOM 之间的 link 由引擎提供。每个浏览器都使用 JavaScript 引擎。例如 Chrome 使用 V8 JavaScript engine. From an introduction of V8:

JavaScript is most commonly used for client-side scripting in a browser, being used to manipulate Document Object Model (DOM) objects for example. The DOM is not, however, typically provided by the JavaScript engine but instead by a browser. The same is true of V8—Google Chrome provides the DOM. V8 does however provide all the data types, operators, objects and functions specified in the ECMA standard.

这如何转化为您的问题?

任何想要显示 JavaScript 应用程序的东西都需要有一个 JavaScript 引擎和一个 DOM。这可以是像 Chrome 这样的浏览器,但也可以是任何其他应用程序。

路由器功能的简单解释是更改 DOM 以显示不同的 "documents"。这么简单地说:任何应用程序,无论是移动应用程序还是桌面应用程序,只要有 DOM 就知道如何使用 Angular 的路由系统。

在浏览器之外意味着您所说的只是应用程序?。 angular 通常绑定到 HTML 个页面。因此,它是一个用于管理(不是完全合适的词)html 页面以将它们变成单页应用程序的框架,这样浏览器就不需要根据单个页面的请求重新加载整个 Web 应用程序,它有助于调用html 页面进入主 html 页面,这使得应用程序不会重新加载整个页面,而是使请求的页面可用。这就是路由的来源。

Angular 在那里工作得很好。事实上,有一个基于 angular 之上的 Ionic 项目。例如。如果您使用的是 Cordova,则该应用程序会在浏览器中呈现(或至少使用浏览器引擎)。据我所知,除了用户无法输入 URL 或单击 back/forward.

外,它的行为方式完全相同

此外,我为浏览器开发了一个应用程序,我尽可能不使用 URL。例如。我只在状态之间转换,我的应用程序中根本没有直接 url。当然,我需要支持到用户可以输入 url 的程度,但如果您正确映射路由,ui-路由器会自行完成。但是对于 SPA 完全不依赖 url 似乎更有益(对于内部的东西,因为你仍然有编辑 url 正如我之前所说)。