javascript - AngularJS 如何异步遍历 Angular 模板(HTML + AngularJS 指令)?

标签 javascript html angularjs asynchronous

我正在阅读 Brad Green 和 Shyama Sheshadari 所著的 AngularJS 书籍,并发现了以下文字:

The basic startup flow looks like this:
1. A user requests the first page of your application.
2. The user’s browser makes an HTTP connection to your server and loads the in dex.html page containing your template.
3. Angular loads into the page, waits for the page to be fully loaded, and then looks for ng-app to define its template boundaries.
4. Angular traverses the template and looks for directives and bindings. This results in registration of listeners and DOM manipulation, as well as fetching initial data from the server. The end result of this work is that the app is bootstrapped and the template is converted into view as a DOM.
5. You connect to your server to load additional data you need to show the user as needed.

Steps 1 through 3 are standard for every Angular app. It’s in steps 4 and 5 that you have choices. These steps can happen synchronously or asynchronously. For performance, the data your app needs to display to the user on the first view can come down with the HTML template to avoid multiple requests.

第 4 步如何异步发生?另外,作者说第一次迭代时数据可以与 HTML 一起发送是什么意思? HTML 中的数据始终来自 Angular 指令,不是吗?

最佳答案

如果 HTML 包含指令,则该指令可能会异步加载模板,例如

至于嵌入数据: 想象一下您的应用程序需要一个用户对象。现在, Controller 可以从服务器加载该数据,以便应用程序可以使用它。但这需要另一个请求。

相反,您可以将该用户对象直接嵌入到页面中,如下所示:

<script ...>
var user = {
            name: 'Me',
            ...
           };

应用程序可以立即使用数据,而无需发出额外的请求。

关于javascript - AngularJS 如何异步遍历 Angular 模板(HTML + AngularJS 指令)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25543929/

相关文章:

javascript - AngularJS 指令上有类似 DependencyProperty 的功能吗?

javascript - 如何在指令中重新加载 css 背景图像

angularjs - 如何将 eventHandlers 和 uploadEvntHandlers 与 angularjs 1 http 请求一起使用?

javascript - Cypress 调用不执行隐藏元素交互的 "show"方法

javascript - isNaN() 与 typecast(varString).tostring() = varString

javascript - 如何更改我的 js 代码以使菜单正常工作

javascript - 悬停在不工作时的动画对象

javascript - 滚动时淡化图像

JavaScript:字符串化实际变量名称

javascript - 如何将发布/获取 json 值 (html) 发送到 API?