是否有可能在角模板做可重复使用的片段? [英] Is it possible to do reusable snippets within Angular templates?
本文介绍了是否有可能在角模板做可重复使用的片段?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我发现自己一次又一次地重复code相同的片段,是有可能做的角度是这样的:
< DIV NG-片断=mySnippet>
这是一个片段
< / DIV>< DIV NG-片断=anotherSnippet>
然而,另一个片段!
< / DIV>< NG:包括SRC =anotherSnippet>
< NG:包括SRC =anotherSnippet>
< NG:包括SRC =mySnippet>
和输出上述将是:
另一个片段!
然而,另一个片段!
这是一个片段
我不一定要找这个确切的NG:包括解决方案或模式,但一些会降低我的模板重复
解决方案
<脚本类型=文/ NG-模板'ID =mySnippet>
这是一个片段
< / SCRIPT><脚本类型=文/ NG-模板'ID =anotherSnippet>
然而,另一个片段!
< / SCRIPT>< NG-包括SRC ='anotherSnippet'>< / NG-包括>
< NG-包括SRC ='anotherSnippet'>< / NG-包括>
< NG-包括SRC ='mySnippet'>< / NG-包括>
这应该是你想要的。
I'm finding myself repeating the same snippets of code again and again, is it possible to do something like this in angular:
<div ng-snippet="mySnippet">
This is a snippet
</div>
<div ng-snippet="anotherSnippet">
Yet another snippet!!
</div>
<ng:include src="anotherSnippet">
<ng:include src="anotherSnippet">
<ng:include src="mySnippet">
and the output of the above would be:
Yet another snippet!!
Yet another snippet!!
This is a snippet
I'm not necessarily looking for this exact "ng:include" solution or pattern but something that would reduce the repetition in my templates.
解决方案
<script type='text/ng-template' id="mySnippet">
This is a snippet
</script>
<script type='text/ng-template' id="anotherSnippet">
Yet another snippet!!
</script>
<ng-include src="'anotherSnippet'"></ng-include>
<ng-include src="'anotherSnippet'"></ng-include>
<ng-include src="'mySnippet'"></ng-include>
This should be what you want.
Docs for script and ng-include.
这篇关于是否有可能在角模板做可重复使用的片段?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文