{"id":14737,"date":"2014-07-13T17:42:18","date_gmt":"2014-07-13T12:12:18","guid":{"rendered":"http:\/\/www.tothenew.com\/blog\/?p=14737"},"modified":"2014-07-13T18:10:24","modified_gmt":"2014-07-13T12:40:24","slug":"demystifying-transclusion","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/demystifying-transclusion\/","title":{"rendered":"Demystifying Transclusion"},"content":{"rendered":"<p><img decoding=\"async\" loading=\"lazy\" src=\"\/blog\/wp-ttn-blog\/uploads\/2014\/12\/angularjs.jpg\" alt=\"angularjs\" width=\"600\" height=\"300\" class=\"aligncenter size-full wp-image-14740\" srcset=\"\/blog\/wp-ttn-blog\/uploads\/2014\/12\/angularjs.jpg 600w, \/blog\/wp-ttn-blog\/uploads\/2014\/12\/angularjs-300x150.jpg 300w\" sizes=\"(max-width: 600px) 100vw, 600px\" \/><\/p>\n<p>Wait, <strong>Trans<\/strong> .. what? Yes, <strong>Transclusion<\/strong> is the <code>mysterious<\/code>, big and most <code>intimidating<\/code> of words when writing <code>AngularJS<\/code> Components. AngularJS, being so powerful and all provides a lot of ways to do many things.<\/p>\n<p>Now writing directives is not a very complex exercise, infact it is quite simple now if you first have a look at the <strong>improving<\/strong>(sort of) documentation. But when writing directives in order to create Re-usable components, then we have to consider a lot of stuff for functionality and customization for our component to be successful. And nothing says customizable when the user can use his own markup inside our component.<\/p>\n<p><h2>So what is it exactly<\/h2>\n<\/p>\n<p>In simple words <strong>transclusion<\/strong> is the ability to wrap user&#8217;s markup by our own template(markup). Lets say we are creating some component in which the user can hack in his own markup with his own scope variables and values. The markup they provide will co-exist in the component we wrote!<\/p>\n<p>Confused, lets consider an example:<\/p>\n<p>[js]<br \/>\nreturn {<br \/>\n\trestrict: &quot;E&quot;,<br \/>\n\ttemplateUrl: &quot;template.html&quot;,<br \/>\n\ttransclude: true,<br \/>\n\tscope: {},\t\/\/lets have some isolation<br \/>\n\tlink: function(scope, attrs) {<br \/>\n\t\tscope.favoriteCharacter = &quot;Tyrion Lannister;<br \/>\n\t}<br \/>\n}<br \/>\n[\/js]<\/p>\n<p>and our <code>template.html<\/code> looks like this:<\/p>\n<p>[js]<br \/>\n&lt;div&gt;<br \/>\n\t&lt;div ng-transclude&gt; This will be replaced by user markup &lt;\/div&gt;<br \/>\n\t&lt;div class=&quot;my-own-component&quot;&gt; {{favoriteCharacter}} is awesome.&lt;\/div&gt;<br \/>\n&lt;\/div&gt;<br \/>\n[\/js]<\/p>\n<p>and we&#8217;ll be using it like this:<\/p>\n<p>[js]<br \/>\n&lt;game-of-thrones&gt;<br \/>\n\t&lt;!&#8211;<br \/>\n\t\tThis following markup will replace the div with<br \/>\n\t\tng-transclude directive<br \/>\n\t&#8211;&gt;<br \/>\n\t&lt;div&gt;What&#8217;s you problem Oberion?, {{oberion}} &lt;\/div&gt;<br \/>\n&lt;\/game-of-thrones&gt;<br \/>\n[\/js]<\/p>\n<p>In the above snippets the two important things to notice are <code>transclude: true<\/code> in our directive and <code>ng-transclude<\/code> in our template markup.<\/p>\n<p>The <code>transclude<\/code> option in our directive tells angular that this directive will be using <em>transclusion<\/em> (As per the docs you should really use this option when you require transclusion capabilities), the second part in the template markup tells the directive that this part is to be replaced.<br \/>\nSimple huh! \ud83d\ude09<\/p>\n<p><h2>But, what about scope?<\/h2>\n<\/p>\n<p>Since its <code>AngularJS<\/code> it really isn&#8217;t complete without <code>scope<\/code> talks. Transclusion does some trickery with the scope.<\/p>\n<p>You must have noticed that we really don&#8217;t have any <code>oberion<\/code> in our directive scope, so how will this work. Well <code>scope<\/code> with transclusion works a bit differently because since end user&#8217;s markup is embedded it would require the context of the user&#8217;s scope tree and not the isolated scope of our directive. <\/p>\n<p>Thus the transcluded element does not refer to the scope of our directive, as if it didn&#8217;t exist. It would always refer to the scope in which the directive is used.<\/p>\n<p>Thus even though <code>oberion<\/code> is not in our isolated scope, it wouldn&#8217;t matter because it will always be picked from outer context. Template markup other than the transcluded section will continue to use the isolated scope.<\/p>\n<p>so if the user&#8217;s end looks like this:<\/p>\n<p>[js]<br \/>\n\/\/ The Controller<br \/>\n\/\/ &#8230;<br \/>\n$scope.oberion = &quot;The mountain makes my head explode&quot;;<br \/>\n$scope.favoriteCharacter = &quot;Khaleesi&quot;;<br \/>\n\/\/ &#8230;<\/p>\n<p>\/\/The markup<br \/>\n&lt;game-of-thrones&gt;<br \/>\n\t&lt;!&#8211;<br \/>\n\t\tThis following markup will replace the div with<br \/>\n\t\tng-transclude directive<br \/>\n\t&#8211;&gt;<br \/>\n\t&lt;div&gt;What&#8217;s you problem Oberion?, {{oberion}} &lt;\/div&gt;<br \/>\n\t&lt;\/game-of-thrones&gt;<br \/>\n\/\/ &#8230;<\/p>\n<p>\/\/ Final rendered markup<br \/>\n&lt;game-of-thrones&gt;<br \/>\n\t&lt;div&gt;What&#8217;s you problem Oberion?, The mountain makes my head explode.&lt;\/div&gt;<br \/>\n\t&lt;div class=&quot;my-own-component&quot;&gt; Tyrion Lannister is awesome.&lt;\/div&gt;<br \/>\n&lt;\/game-of-thrones&gt;<\/p>\n<p>[\/js]<\/p>\n<p>The above is a very simple way to understand <code>transclusion<\/code>, The basics are really easy to grasp and implement even though the term sounds very <code>complicated<\/code> and <code>scientific<\/code>.<\/p>\n<p>Hope you enjoyed this little look at <strong>Transclusion<\/strong> in AngularJS.<\/p>\n<p>Happy Transcluding \ud83d\ude42<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In simple words transclusion is the ability to wrap user&#8217;s markup by our own template(markup). Lets say we are creating some component in which the user can hack in his own markup with his own scope variables and values. The markup they provide will co-exist in the component we wrote!<\/p>\n","protected":false},"author":85,"featured_media":14740,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":2},"categories":[1],"tags":[955,1155,1480],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/14737"}],"collection":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/users\/85"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/comments?post=14737"}],"version-history":[{"count":0,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/14737\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media\/14740"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=14737"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=14737"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=14737"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}