I am Fairly new to Angular but have been reading quite a lot.
I was reading about ng-transclude
at http://docs.angularjs.org/guide/directive#creating-custom-directives_demo_isolating-the-scope-of-a-directive and I think I understand properly what it does.
If you have a directive that applies to an element that has content inside it such as in
<my-directive>directive content</my-directive>
It will allow you to tag an element within the directive's template with ng-transclude
and the content included in the element would be rendered inside the tagged element.
so if the template for myDirective
is <div>before</div><div ng-transclude></div><div>after</div>
it would render as beforedirective contentafter.
this is all o.k. my Q is is it possible to somehow pass more then a single block of html into my directive?
e.g.
suppose the directive usage would look like this:
<my-multipart-directive>
<part1>content1</part1>
<part2>content2</part2>
</my-multipart-directive>
and have a template like:
<div>
this: <div ng-transclude="part2"></div>
was after that: <div ng-transclude="part1"></div>
but now they are switched
<div>
render as
<div>
this: <div ng-transclude="part2">content2</div>
was after that: <div ng-transclude="part1">content1</div>
but now they are switched
<div>
?
(thinking to myself) Could I somehow bind the HTML value of a node to the model so that I will be able to use it in such a way without calling it "transclude" ...
Thanks
Cool question. I'm not sure there is a built in way, but you can do it yourself in a pretty generic way.
You can access the transcluded element by passing in the $transclude service like this:
Where clone will be a copy of the pre-linked transcluded content. Then if you label the content in the element like this:
You can loop over the content and compile it like this:
Great! now you just need to put the content in the correct place in your template
Then you can in your link function assign the content correctly
I set up a fiddle you can play with that has this set up.
Hope this helped!
In our project we have modeled multi site trasclusion after JSF 2's ui:composition, ui:insert, ui:define (see ui:composition).
Implementation consists of three simple directives: ui-template, ui-insert, ui-define (see angularjs-api/template/ui-lib.js).
To define a template one writes the following markup (see angularjs-api/template/my-page.html):
and declares a directive (see angularjs-api/template/my-page.js):
and finally, to instantiate the directive one needs to write (see angularjs-api/template/sample.html):
The working sample can be seen through rawgit: sample.html
See also: Multisite Transclusion in AngularJS
Starting Angular 1.5, it's now possible to create multiple slots. Instead of transclude:true, you provide an object with the mappings of each slot:
https://docs.angularjs.org/api/ng/directive/ngTransclude