Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I recommend a directive for a couple of reasons:</p> <ul> <li>it can be reused and parameterized in the HTML (i.e., "every 3rd thing" can be a directive attribute)</li> <li>it does not require any controller code/$scope properties, and hence it does not require recalculation of controller $scope properties if the "things" array changes in size</li> </ul> <p>Here is a suggested element directive:</p> <pre><code>&lt;row-generator row-data=objects col-count=3&gt;&lt;/row-generator&gt; </code></pre> <p>In the implementation I used code similar to your server-side example:</p> <pre><code>myApp.directive('rowGenerator', function() { var rowTemplate = '&lt;div class="row"&gt;', colTemplate = '&lt;div class="span4"&gt;'; return { restrict: 'E', // use '=' for colCount instead of '@' so that we don't // have to use attr.$observe() in the link function scope: { rowData: '=', colCount: '='}, link: function(scope, element) { // To save CPU time, we'll just watch the overall // length of the rowData array for changes. You // may need to watch more. scope.$watch('rowData.length', function(value) { var html = rowTemplate; for(var i=0; i &lt; scope.rowData.length; i++) { html += colTemplate + scope.rowData[i].key + '&lt;/div&gt;'; if (i % scope.colCount == scope.colCount - 1) { html += '&lt;/div&gt;' + rowTemplate; } } html += '&lt;/div&gt;'; // don't use replaceWith() as the $watch // above will not work -- use html() instead element.html(html); }) } } }); </code></pre> <p>Data:</p> <pre><code>$scope.things = [ {key: 'one'}, {key: 'two'}, {key: 3}, {key: 4}, {key: 'five'}, {key: 'six'}, {key: 77}, {key: 8} ]; </code></pre> <p><a href="http://jsfiddle.net/mrajcok/K6uFx/" rel="nofollow">Fiddle</a></p> <p>To be efficient, the directive as shown only looks for changes to the length of the rowData (i.e., things) array. If you want to have the directive update the view if the value of one of the array elements changes, you'll need a more expensive $watch:</p> <pre><code>scope.$watch('rowData', function(value){ ... }, true) </code></pre> <p>The <code>true</code> at the end does "shallow" dirty checking ("compares the object for equality rather than for reference) -- see <a href="http://docs.angularjs.org/api/ng.%24rootScope.Scope#%24watch" rel="nofollow">docs</a>.</p> <p>There's one thing I don't like about the directive -- it needs to know that rowData entries have a property with name <code>key</code>:</p> <pre><code>html += colTemplate + scope.rowData[i].key + '&lt;/div&gt;'; </code></pre>
    singulars
    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. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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