我有一個頁面加載時調用的api。來自api的數據通過角度重複加載到表格中。我也有一個JavaScript函數每10秒調用一次,調用同一個數據集的api。我想知道如何將新數據集應用到表中,並在數據集更改時替換舊數據,以及如何使用動畫直觀顯示此更改。代碼如下。角度表刷新
表代碼
<body ng-app="myApp" ng-controller="ScansController">
<div class="bs-example" id="scan-table">
<table id="scansTable" class="table table-striped">
<thead>
<tr>
<th>ScanId</th>
<th>First Name</th>
<th>Last Name</th>
<th>Time Stamp</th>
</tr>
<tr ng-repeat="scan in Scans">
<td>
{{scan.scanId}}
</td>
<td>
{{scan.firstName}}
</td>
<td>
{{scan.lastName}}
</td>
<td>
{{scan.timeStamp}}
</td>
</tr>
</thead>
</table>
</div>
javascipt的間隔碼
<script>
window.setInterval(function() {
$.ajax({
url: 'api/scans/',
type: 'Get',
dataType: 'json',
success: function (data) {
//Something here
},
error: function() {
alert("something failed");
}
});
}, 10000);
</script>
角代碼
var myApp = angular.module('myApp', []);
myApp.service('dataService', function ($http) {
this.getData = function() {
return $http({
method: 'GET',
url: '/api/scans/'
});
}
});
myApp.controller('ScansController', function ($scope, dataService) {
$scope.Scans = [];
dataService.getData().then(function (result) {
$scope.Scans = result.data;
console.log(result.data);
});
});
當在該模型中的更改會自動對視圖中右鍵影響? Angular會照顧那個嗎? https://jsfiddle.net/eu81273/pPU4m/ 看到這個鏈接 –