我发现自己一次又一次地重复相同的代码片段,可以在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:包括”解决方案或模式,但是可以减少我模板中重复的东西。

有帮助吗?

解决方案

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

其他提示

这听起来像你想要使用指令。这是一个简单的例子: http://jsfiddle.net/gyf6v/1/

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top