這不是一個關於我陷入困境的問題。這是一個關於良好實踐和表現的問題。我的代碼工作正常。但我想知道做這個任務的正確方法。也許我是對的。只要看看場景和代碼,請給我建議正確的方法。Angular js同時從多個表中獲取數據
要求
我有近數據庫約20桌,當用戶登錄並轉至儀表板頁面,我需要從所有表顯示的數據。不需要顯示所有數據,因此我使用Angular $ http從每個表中獲取25行並在儀表板頁面中顯示。
代碼
我的角的js代碼是:
$scope.$on('$viewContentLoaded', function (event) {
$scope.loadDashboardQue(event, '/route1/getDashboardData?user_id=123', 'table1');
$scope.loadDashboardQue(event, '/route2/getDashboardData?user_id=123', 'table2');
$scope.loadDashboardQue(event, '/route3/getDashboardData?user_id=123', 'table3');
$scope.loadDashboardQue(event, '/route4/getDashboardData?user_id=123', 'table4');
$scope.loadDashboardQue(event, '/route5/getDashboardData?user_id=123', 'table5');
$scope.loadDashboardQue(event, '/routeN/getDashboardData?user_id=123', 'tableN');
});
現在loadDashboardQue功能界定及
$scope.loadDashboardQue = function (event, url, tableName) {
$http.get(url)
.success(function (response) {
if (response.status === 'success') {
//Assigning data to HTML table
if (tableName === 'table1') {
$scope.table1Data = response.data;
}
if (tableName === 'table2') {
$scope.table2Data = response.data;
}
if (tableName === 'table3') {
$scope.table3Data = response.data;
}
if (tableName === 'table4') {
$scope.table4Data = response.data;
}
if (tableName === 'table5') {
$scope.table5Data = response.data;
}
if (tableName === 'tableN') {
$scope.tableNData = response.data;
}
} else {
console.log("Something wrong while fetching data from table ", tableName);
}
})
.error(function (error) {
console.log("The error is :", err);
});
});
HTML表格
<table style="width:100%">
<tr>
<th>Nme</th>
<th>Id</th>
<th>Contact No</th>
</tr>
<!--Table1 Data-->
<tr ng-repeat="data in table1Data">
<td>{{ data.user_name }}</td>
<td>{{ data.user_id }}</td>
<td>{{ data.mobile }}</td>
</tr>
<!--Table2 Data-->
<tr ng-repeat="data in table2Data">
<td>{{ data.customerName }}</td>
<td>{{ data.customerId }}</td>
<td>{{ data.phone }}</td>
</tr>
<!--Table3 Data-->
<tr ng-repeat="data in table3Data">
<td>{{ data.student_name }}</td>
<td>{{ data.roll_no }}</td>
<td>{{ data.mobile }}</td>
.
.
.
<!--TableN Data-->
<tr ng-repeat="data in tableNData">
<td>{{ data.developer_name }}</td>
<td>{{ data.id }}</td>
<td>{{ data.mobile }}</td>
</tr>
</table>
您可以在每張表格中看到列名稱不同,因此我無法在單個ng-repeat
中顯示所有數據。所以我已經爲每個表格分別編寫了ng-repeat
。
此代碼工作正常,但當頁面開始加載它需要超過7秒所以這是我的擔心(性能明智)。所以請建議我是否有更好的方法來實現這一點。
感謝您寶貴的時間。