Created

Embed URL

HTTPS clone URL

SSH clone URL

You can clone with HTTPS or SSH.

Download Gist

Set repeat is an AngularJS directive to iterate over a group of elements only one time and not add any watch listeners. It works the same way as ng-repeat and uses angular templating engine to render it's results. i.e. set-repeat="message in messages" where messages is an array of objects. This iteration will not update if the length of the array changes or any of the properties in any of the objects change it is static.

View set-repeat.js
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
angular.module('setRepeat',[]).directive('setRepeat', function () {
 
return {
transclude: 'element',
priority: 1000,
compile: compileFun
};
 
function compileFun(element, attrs, linker) {
var expression = attrs.setRepeat.split(' in ');
expression = {
child : expression[0],
property : expression[1]
};
 
return {
post: repeat
};
 
function repeat(scope, iele, iattrs /*, attr*/) {
var template = element[0].outerHTML;
var data = scope.$eval(expression.property);
addElements(data,scope,iele);
 
return;
 
function makeNewScope (index, expression, value, scope, collection) {
var childScope = scope.$new();
childScope[expression] = value;
childScope.$index = index;
childScope.$first = (index === 0);
childScope.$last = (index === (collection.length - 1));
childScope.$middle = !(childScope.$first || childScope.$last);
/**
*
* uncomment this if you want your children to keep listening for changes
*
**/
//childScope.$watch(function updateChildScopeItem(){
//childScope[expression] = value;
//});
return childScope;
}
 
function addElements (collection, scope, insPoint) {
var frag = document.createDocumentFragment();
var newElements = [], element, idx, childScope;
 
angular.forEach(data, function(v,i){
childScope = makeNewScope(i,expression.child,v,scope,collection);
element = linker(childScope, angular.noop);
newElements.push(element);
frag.appendChild(element[0]);
});
 
insPoint.after(frag);
return newElements;
}
}
}
 
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.