Angularjs 使用 routeProvider 时自动刷新数据
Angularjs automatic refresh for data while using routeProvider
我正在尝试在 time.The 的时间间隔内更新 table 上的数据代码有一个带有 URL 的 routeProvider 和如下所示的控制器。我正在使用 init()
来获取控制器中的数据。如果我在 $interval() 中使用 init()
方法,table 会更新为以前的值。我的意思是,如果没有新值并且有 10 条记录,它就会变成 20 条记录。如何单独更新新值。
最小app.js代码
App.controller('PlantCtrl', [ '$scope', 'PlantDetails', '$interval'
, function($scope, PlantDetails, $interval,) {
$scope.availablePlantDetailsTemp = [];
$scope.availablePlantDetails = [];
init();
function init() {
$scope.availablePlantDetailsTemp = PlantDetails.resource2.query(function(response) {
angular.forEach(response, function(item) {
if (item) {
$scope.availablePlantDetails.push(item);
}
});
});
}
$interval(function() {
init();
}, 5000);
} ]);
或者如果我在 routeProvider 中使用 resolve 进行查询,我如何调用 $interval()
中的方法
您只需要在向数组添加新值之前清除数组即可。数组 push
方法向数组添加新值,因此您遇到了问题。
function init() {
$scope.availablePlantDetails = [];
$scope.availablePlantDetailsTemp = PlantDetails.resource2.query(function(response) {
angular.forEach(response, function(item) {
if (item) {
$scope.availablePlantDetails.push(item);
}
});
});
$interval(function(){
Init()
},1*2000)
我正在尝试在 time.The 的时间间隔内更新 table 上的数据代码有一个带有 URL 的 routeProvider 和如下所示的控制器。我正在使用 init()
来获取控制器中的数据。如果我在 $interval() 中使用 init()
方法,table 会更新为以前的值。我的意思是,如果没有新值并且有 10 条记录,它就会变成 20 条记录。如何单独更新新值。
最小app.js代码
App.controller('PlantCtrl', [ '$scope', 'PlantDetails', '$interval'
, function($scope, PlantDetails, $interval,) {
$scope.availablePlantDetailsTemp = [];
$scope.availablePlantDetails = [];
init();
function init() {
$scope.availablePlantDetailsTemp = PlantDetails.resource2.query(function(response) {
angular.forEach(response, function(item) {
if (item) {
$scope.availablePlantDetails.push(item);
}
});
});
}
$interval(function() {
init();
}, 5000);
} ]);
或者如果我在 routeProvider 中使用 resolve 进行查询,我如何调用 $interval()
中的方法您只需要在向数组添加新值之前清除数组即可。数组 push
方法向数组添加新值,因此您遇到了问题。
function init() {
$scope.availablePlantDetails = [];
$scope.availablePlantDetailsTemp = PlantDetails.resource2.query(function(response) {
angular.forEach(response, function(item) {
if (item) {
$scope.availablePlantDetails.push(item);
}
});
});
$interval(function(){
Init()
},1*2000)