是否可以在AngularJS模板中执行可重用的片段? [英] Is it possible to do reusable snippets within AngularJS templates?

查看:82
本文介绍了是否可以在AngularJS模板中执行可重用的片段?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发现自己一遍又一遍地重复相同的代码片段,是否有可能在AngularJS中执行类似的操作:

I'm finding myself repeating the same snippets of code again and again, is it possible to do something like this in AngularJS:

<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">

以上输出为:

Yet another snippet!!
Yet another snippet!!
This is a snippet

我不一定非常期待ng:include 解决方案或模式,但会减少模板中的重复。

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>

这应该是你想要的。

脚本 ng-include

这篇关于是否可以在AngularJS模板中执行可重用的片段?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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