我目前有一個應用程序,當點擊一個按鈕時,它會發送一個$ http請求並返回一些信息。然後用所述數據更新變量rows
和columns
。 ng-repeat
繼續循環瀏覽我的所有數據。Angular JS在使用ng-repeat遍歷海量數據集時凍結DOM
當我增加數據超過1000個對象時,它完全鎖定了DOM。我如何優化我的代碼,使其不鎖定DOM 有史以來?這一點非常重要,因爲我的用戶需要能夠在數據迭代完成時在頁面上執行操作。我需要假設的解決方案來處理多達10000行。
我的HTML ...
<table>
<tr>
<th ng-repeat="column in columns | orderBy:columnToSortBy:reverse">
<h6 ng-bind="column.name" ng-click='sortBy(column.name)'></h6>
<img src='/static/images/asc.gif' ng-show="orderByAsc">
<img src='/static/images/desc.gif' ng-show="orderByDesc">
<img src='/static/images/bg.gif' ng-show="orderByNothing">
</th>
</tr>
<tr ng-repeat="row in rows">
<td ng-repeat="(key, value) in row" ng-bind="value"></td>
</tr>
</table>
這裏是控制器,其中HTML被附加正在創建,追加,並且$compile
「編...
mainQueryModule.controller("newQueryController", function($scope, $http, $compile){
var results = {columns:[{"type": "STRING", "name": "nk"}, {"type": "STRING", "name": "Bn"}, {"type": "STRING", "name": "Sk"},{"type": "STRING", "name": "ank"}, {"type": "STRING", "name": "Bnk"}, {"type": "STRING", "name": "Skk"}, {"type": "STRING", "name": "Name"}, {"type": "STRING", "name": "Phone"}, {"type": "STRING", "name": "Age"}, {"type": "STRING", "name": "Tank"}, {"type": "STRING", "name": "Bank"}, {"type": "STRING", "name": "Skunk"}], rows:[]};
for(index = 0; index < 1000; index++){
results['rows'].push({nk: "select * from faker 100000000000", Bn: 35, Sk: 567, ank: "ank", Bnk: "Bnk", Skk: "Skk", Name: "Erik", Phone: 100000+index, Age: 29+index, Bank: "Big"+index, Tank: 16+index, Skunk: "123"+index})
}
$scope.runQuery = (client_id) => {
// $http.post("fakeurl/", {test:123}).then(function(response){
// console.log(response)
$scope.columns = results["columns"];
$scope.columnToSortBy = null;
$scope.rows = results["rows"];
$scope.reverse = false;
$scope.orderByAsc = false;
$scope.orderByDesc = false;
$scope.orderByNothing = true;
// })
}
})
根據@Gaurav建議反映變化的更新問題。
使用web worker。並在後臺運行 –
爲什麼你需要1000行?使用分頁,用戶無法處理該行數。這將編譯更少的信息,並且DOM也不會凍結 –
也更好地使用指令,並且不會從控制器處理dom。讓控制器只需使用$ http請求數據並創建結果行,並讓您的模板自己生成html –