gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 08:00:08 27 4
gpt4 key购买 nike

我正在阅读 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/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com