我的代码是从API中获取数据,该API始终从数据库获取数据。我有一个带有ng-submit的按钮,它发送序列号并使用GET动词来获取数据。但是我想继续更新显示的数据。因此,我在第一次检索时使用间隔并使用相同的功能。运行$ interval时,它还会返回以前的序列号数据。
HTML
<form ng-submit="main.loadData()">
<div class="input-group">
<span class="input-group-addon stncolor" id="basic-addon1"><i class="fa fa-barcode" aria-hidden="true"></i></span>
<input type="text" class="form-control" placeholder="Servicetag" maxlength="7" aria-describedby="basic-addon1" ng-model="main.servicetag">
<div class="input-group-btn">
<button type="submit" class="btn btn-primary">Load</button>
</div>
</div>
</form>
CONTROLLER
(function(){
angular.module('mainApp', [])
.controller('mainController', mainController);
mainController.$inject = ['$http', '$interval']
function mainController($http, $interval){
var self = this;
self.loadData = function(){;
getServiceTagData(self.servicetag);
refresh(self.servicetag);
};
// Get initial Data
var getServiceTagData = function(stn){
$http.get("http://XXX.XXX.XXX.XXX/NPIAPI/api/GetAlltestData/" + stn)
.then(function(response){
self.localizationdata = response.data;
}, function(response){
console.log(response.data);
});
};
// Refresh Data from localization
var refresh = function(stn){
$interval(function(){
getServiceTagData(stn);
console.log("Refreshed Localization Data");
},30000);
};
}
})();
When checking one serial number
When checking another serial number, previous returned array is present as well as new array