Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to inject an html tag from database into my directive's template, in effect dynamically defining the root element/tag of the template?
    primarykey
    data
    text
    <p>I want to dynamically build a template with data from database (Firebase/angularFire in my case). The way the template will be built depends on the tag property within each object retrieved with angularFire. My first approach:</p> <pre><code>var imgTemplate = function(tag){ return '&lt;'+tag.tag+' class="{{ tag.classes }}" src="{{ tag.content }}" alt="{{ tag.alt }}"&gt;'; }; var txtTemplate = function(tag){ return '&lt;'+tag.tag+' class="{{ tag.classes }}"&gt;{{ tag.content }}&lt;/'+tag.tag+'&gt;'; }; </code></pre> <p>This almost works, but the tag.tag is undefined instead of the actual tag, which is understandable, as the compile function which uses these functions will run before angularFire fetches and binds the data. Here's my second approach:</p> <pre><code>var imgTemplate = function(tag){ return '&lt;{{tag.tag}} class="{{ tag.classes }}" src="{{ tag.content }}" alt="{{ tag.alt }}"&gt;'; }; var txtTemplate = function(tag){ return '&lt;{{ tag.tag }} class="{{ tag.classes }}"&gt;{{ tag.content }}&lt;/{{ tag.tag }}&gt;'; }; </code></pre> <p>But this repeatedly renders only part of the txtTemplate's literal string:</p> <pre><code>&lt;{{ tag.tag }} class="{{ tag.classes }}"&gt;{{ tag.content }} </code></pre> <p>for every 'tag' element iterated over with ng-repeat instead of the actual html.</p> <p>This is the compile function of my directive:</p> <pre><code> compile: function(templateElem, directiveAttrs){ var tag = directiveAttrs.tag; if(tag.tag === 'img'){ var img = angular.element(imgTemplate(tag)); templateElem.replaceWith(img); }else{ var txt = angular.element(txtTemplate(tag)); templateElem.replaceWith(txt); } }, </code></pre> <p>the data is fetched from Firebase in a controller of a angular.ui.router's state:</p> <pre><code>.state("siteNav.sideNav.content", { url: '/:lang/:view/:contentID/:index', views:{ '@': { templateUrl: '/views/content.html', controller: function($scope, angularFire){ var stateParams = $scope.$stateParams; console.log(stateParams.lang); var url = 'https://example.firebaseio.com/'+stateParams.lang+'/content/'+stateParams.view+'/'+stateParams.contentID; angularFire(new Firebase(url), $scope, 'tags'); } } } } </code></pre> <p>And the template content.html looks like this:</p> <pre><code>&lt;div ng-repeat="tag in tags"&gt; &lt;acms-view tag="tag"&gt;&lt;/acms-view&gt; &lt;/div&gt; </code></pre> <ul> <li>I do need the three-way binding offered by angularFire.</li> <li>The tag must be sourced from the firebase, can't use literal instead (in the final version this tag could be anything really).</li> </ul> <p>How can I ensure that the 'tag' property will be populated before compile function uses it for comparison/value assignment?<br> Can't use angularFire service within compile function since there is no scope to bind the fetched data to.<br> For some reason angularFire doesn't work within the linking function, though even if it were, I don't think it would be much of a help, as I still need to build the template.<br> Should I use Firebase's js api within the compile function instead of angularFire?<br> If so, how would I make the three way binding work angularFire offers? <br> Thank you for your time<br> Jared</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload