我什么时候需要使用ng-template? [英] When would I need to use ng-template?

查看:80
本文介绍了我什么时候需要使用ng-template?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在努力寻找 ng-template 以及何时需要的地方.在看了一些教程之后,我仍然不是一个明智的人.

I've been trying to get my head around ng-template and where and when I may need it. After looking at a few tutorials I am still none the wiser.

如果我已经定义了模板,例如

If I have defined a template eg.

<script type="text/ng-template" id="my-template-id">
    ... html here
</script>

我已经看到,有时它与 ng-include 一起使用,有时作为指令中的templateUrl使用.

I have seen that sometimes this is used with ng-include and sometimes as a templateUrl in a directive.

另外,一旦我读到它进入了 $ templateCache ,使用该功能有什么好处?我正在使用Angular动态列出产品,没有太大的动摇,但是如果我可以通过使用类似 ng-template 的东西获得更高的性能,那么我想尝试一下.

Also once I read that it goes in to the $templateCache, what benefit do I get from using this? I am using Angular to dynamically list products, no great shakes there but if I can get more performance from using something like ng-template I would like to have a go at it.

推荐答案

脚本类型 ng-template 用于内嵌html片段.然后, ng-include 或指令可以使用这些片段来加载html片段并构造视图.

Script types ng-template are used to embed html fragments inline. These fragments can then be used by ng-include or directives to load the html fragment and construct the view.

内联模板片段的优势在于,在标准情况下,对服务器加载html模板的调用减少了一个.考虑到模板已缓存以备将来使用,这并不是一个巨大的优势.

Advantage of inline template fragments is that there is one less call to make to server to load the html template which is required in standard scenario. Not a huge advantage considering the template is cached for future use.

缺点是您可能根本不会使用内联模板,而只是将其作为父视图内容的一部分进行加载.

The disadvantage could be that you may never use the inline template at all and it just gets loaded as part of parent view content.

这篇关于我什么时候需要使用ng-template?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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