I've tried many different things to fix performance issues of ng-repeat
.
including stuff described here: How to 'unwatch' an expression
I need to have a large set of rows on the page up to ~1000 rows. With every row containing quite a bit of stuff. And it seems to me now, it's just would be very slow with ng-repeat
, I guess I have to build either my own custom ng-repeat
or I have to build a directive that will build every single row in the table... I don't know how to do either. Can you guys help me please. Can you show me some examples.
Here is an example of populating a <dl> with <dt>s and <dd>s ...
// binds to $scope.details = [] //array object
angular.module('widget.product.details',[])
.directive('productDetails',function(){
return {
template:'<dl class="dl-horizontal"></dl>',
replace:true,
restrict:'E',
compile : function compile(tElement, tAttrs, transclude) {
return {
post: createProductDetails
}
}
}
});
var createProductDetails = function (scope, iElement, iAttrs, controller) {
scope.$watch('details', function(newVal, oldVal) {
angular.forEach(newVal, function(v,k){
iElement.append( angular.element('<dt>'+v.dt+'</dt><dd>'+v.dd+'</dd>') );
});
});
}
<div class="span7" ng-controller="ProductInfoCtrl">
<product-details></product-details>
</div>
function ProductInfoCtrl($scope) {
$scope.details = [
{dt:'condition',dd:'brand new'},
{dt:'year bought',dd:'3 years ago'},
]
}
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;
}
}
}
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With