Javascript 角度ng重复作为静态内容(性能问题)
我在Javascript 角度ng重复作为静态内容(性能问题),javascript,angularjs,performance,angularjs-ng-repeat,Javascript,Angularjs,Performance,Angularjs Ng Repeat,我在中使用嵌套的ng repeat生成一个包含动态内容的表(表的内容由后端发送,不可预测) 由于该表已经在一个指令中,该指令本身使用了一个ng repeat,因此如果我想引起任何更改,我可以请求一个具有新id的新表 问题是,当这个表大约有1000行长时,或者如果我在接口中加载了多个表,那么一切都会变得滞后 出于测试目的,我创建了如下html表: for(var line = 0; line < $scope.content.tableBody.length; line++){ te
中使用嵌套的ng repeat
生成一个包含动态内容的表(表的内容由后端发送,不可预测)
由于该表已经在一个指令中,该指令本身使用了一个ng repeat
,因此如果我想引起任何更改,我可以请求一个具有新id的新表
问题是,当这个表大约有1000行长时,或者如果我在接口中加载了多个表,那么一切都会变得滞后
出于测试目的,我创建了如下html表:
for(var line = 0; line < $scope.content.tableBody.length; line++){
testTable += "<tr>"
for(var cell = 0; cell < $scope.content.tableHead.length; cell++){
testTable += "<td style='background-color: WHITE'>"
testTable += $scope.content.tableBody[line][$scope.content.tableHead[cell]].value
testTable += "</td>"
}
testTable += "</tr>"
}
for(var line=0;line<$scope.content.tableBody.length;line++){
测试表+=“”
对于(变量单元格=0;单元格<$scope.content.tableHead.length;单元格++){
测试表+=“”
testTable+=$scope.content.tableBody[行][$scope.content.tableHead[单元格]].value
测试表+=“”
}
测试表+=“”
}
并使用ng bind html
来显示它。性能差异很大。当然,我不想失去angular在两者之间提供的所有便利(如ng class
,ng style
,ng if
),但似乎ng repeat
生成的所有作用域都会导致性能下降。我尝试使用一个绑定:
,跟踪$index
等等,但没有成功
是否可以使用
ng repeat
并在加载所有内容后,将其内容设置为“静态”?(在我们的范围内,$watcher等)尝试通过使用一次性绑定和track by$index
解决我在ng repeat中的一些调整后,我发现的解决方案是连接一个字符串,生成一个tbody
,其逻辑与我之前在两个嵌套ng repeat中使用的逻辑相同。然后,我使用在另一篇文章中找到的指令将这个字符串“嵌入”到html中
delph.directive('compile', ['$compile', function ($compile) {
return function(scope, element, attrs) {
var listener = scope.$watch(
function(scope) {
// watch the 'compile' expression for changes
return scope.$eval(attrs.compile);
},
function(value) {
// when the 'compile' expression changes
// assign it into the current DOM
element.html(value);
// compile the new DOM and link it to the current
// scope.
// NOTE: we only compile .childNodes so that
// we don't get into infinite loop compiling ourselves
$compile(element.contents())(scope);
}
);
};
}]);
内存消耗有了很大的改善这是Angular 1的缺点之一:因为它是高级的,所以速度很慢。。。我不相信这里有很多事情要做,除了切换到Angular 2或React:)如果列表是静态的,请使用一次性绑定。它大大提高了性能。像
是的,一次性绑定帮助。此外,您可以尝试使用$compile将指令呈现为静态html。