Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to do reusable snippets within AngularJS templates?

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

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.

like image 811
DaveJ Avatar asked Oct 02 '12 21:10

DaveJ


People also ask

What are templates in AngularJS?

In AngularJS, templates are written with HTML that contains AngularJS-specific elements and attributes. AngularJS combines the template with information from the model and controller to render the dynamic view that a user sees in the browser.

Is AngularJS a template engine?

AngularJS is client-side template rendering while Express is server-side.

How Angular templates work?

Each Angular template in your application is a section of HTML to include as a part of the page that the browser displays. An Angular HTML template renders a view, or user interface, in the browser, just like regular HTML, but with a lot more functionality.


1 Answers

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

like image 51
Renan Tomal Fernandes Avatar answered Sep 21 '22 11:09

Renan Tomal Fernandes