Is it possible to do reusable snippets within AngularJS templates?

前端 未结 2 541
粉色の甜心
粉色の甜心 2020-12-29 21:59

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

相关标签:
2条回答
  • 2020-12-29 22:07

    This sounds like you want to use directives. Here is a simple example: http://jsfiddle.net/gyF6V/1/

    0 讨论(0)
  • 2020-12-29 22:21
    <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.

    0 讨论(0)
提交回复
热议问题