Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>This first one is a directive that evaluates whether something should be in the DOM only once and adds no watch listeners to the page:</p> <pre><code>angular.module('setIf',[]).directive('setIf',function () { return { transclude: 'element', priority: 1000, terminal: true, restrict: 'A', compile: function (element, attr, linker) { return function (scope, iterStartElement, attr) { if(attr.waitFor) { var wait = scope.$watch(attr.waitFor,function(nv,ov){ if(nv) { build(); wait(); } }); } else { build(); } function build() { iterStartElement[0].doNotMove = true; var expression = attr.setIf; var value = scope.$eval(expression); if (value) { linker(scope, function (clone) { iterStartElement.after(clone); clone.removeAttr('set-if'); clone.removeAttr('wait-for'); }); } } }; } }; }); </code></pre> <p>This second one is a directive that conditionally applies attributes to elements only once without watch listeners:</p> <p>i.e.</p> <pre><code>&lt;div set-attr="{ data-id : post.id, data-name : { value : post.name, condition : post.name != 'FOO' } }"&gt;&lt;/div&gt; angular.module('setAttr',[]).directive('setAttr', function() { return { restrict: 'A', priority: 100, link: function(scope,elem,attrs) { if(attrs.setAttr.indexOf('{') != -1 &amp;&amp; attrs.setAttr.indexOf('}') != -1) { //you could just angular.isObject(scope.$eval(attrs.setAttr)) for the above but I needed it this way var data = scope.$eval(attrs.setAttr); angular.forEach(data, function(v,k){ if(angular.isObject(v)) { if(v.value &amp;&amp; v.condition) { elem.attr(k,v.value); elem.removeAttr('set-attr'); } } else { elem.attr(k,v); elem.removeAttr('set-attr'); } }); } } } }); </code></pre> <p>Of course your can use dynamic versions built into angular:</p> <pre><code>&lt;div ng-class="{ 'myclass' : item.iscool }"&gt;&lt;/div&gt; </code></pre> <p>You can also use the new ng-if added by angularjs which basically replaces ui-if created by the angularui team these will conditionally add and remove things from the DOM and add watch listeners to keep evaluating:</p> <pre><code>&lt;div ng-if="item.iscool"&gt;&lt;/div&gt; </code></pre>
 

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