27

同じコードのスニペットを何度も繰り返していることに気づきました.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」ソリューションまたはパターンを必ずしも探しているわけではありませんが、テンプレートの繰り返しを減らすものを探しています。

4

2 に答える 2

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

これはあなたが望むものでなければなりません。

scriptおよびng-include のドキュメント。

于 2012-10-02T22:54:20.830 に答える
12

これは、ディレクティブを使用したいようです。簡単な例を次に示します。http://jsfiddle.net/gyF6V/1/

于 2012-10-02T21:43:44.257 に答える