Angularjs:加载HTML模板时的HTTP请求数? [英] Angularjs: Number of HTTP requests when loading an HTML template?

查看:116
本文介绍了Angularjs:加载HTML模板时的HTTP请求数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚开始学习角所以它可能是一个愚蠢的问题还是我只是失去了一些东西。

I just started learning angular so it might be a silly question or I am just missing something.

我说,我有一个主模板index.html,然后我要插入一个局部模板(这是一个表)在某个时间点。所以我做一个HTTP请求加载的局部与阿贾克斯,然后我也使另一个请求得到JSON数据来填充表部分。
这究竟是怎么运作的?

Say, I have a main template index.html and I want to insert a partial template (which is a table) at some point in time. So I make one HTTP request to load the partial with ajax and then I also make another request to get the JSON data to fill up the table partial. Is this how it works?

当您在服务器端处理它,你得到的数据和变量洒模板您响应前。所以,只有一个请求,并且客户端获取部分已与数据填满。

When you deal with it on the server side, you get the data and sprinkle your template with variables before you respond. So there is only one request and client gets the partial already filled up with data.

我缺少的东西,或者是它是如何,你会得到两次前往该服务器:一个获取数据和其他得到模板

Am I missing something or that's how it is and you get two trips to the server: one to get the data and the other to get the template?

感谢

推荐答案

是的,这是如何工作的开箱即用,但有几种方法,以避免做模板额外的请求。角有一个模板缓存它会尝试之前找到指定的模板它的请求。这是相当常见的,当一个角的应用程序打包进行生产,将生成一个脚本,把所有的模板到缓存中,然后该脚本可以被附加到一切最小化后,该应用程序的其余部分。

Yes, that is how it works out of the box, but there are several ways to avoid having to do that extra request for the template. Angular has a template-cache where it will try to find the given template before it does a request. It's quite common that when an Angular app is packaged for production, a script is generated that puts all the templates into the cache and that script can then be appended to the rest of the application after everything is minimized.

有咕噜-插件,可以为你做这个(例如这个)。我们坚持与行家在我目前所以我做我自己的项目。

There's grunt-plugins that can do this for you (for example this one). We're stuck with maven at the project I'm currently on so I made my own.

另一种方法是在您的index.html文件模板(这显示与<脚本类型=文/ NG-模板ID =templateId.html> 块的文件中的 $ templateCache

Another approach is to include the templates in your index.html file (this is shown with the <script type="text/ng-template" id="templateId.html"> block in the documentation for $templateCache.

这篇关于Angularjs:加载HTML模板时的HTTP请求数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆