我想从此页面http://orbit5.ds.cs.umu.se:8888/vrio/debug/blk获取一些数据。每次刷新页面时,此页面上的数据都会更新。所以我想拥有这些更新的值。为了更新值,我可以使用$ timeout但是我需要以某种方式在getdata之外获取这些值,因为我想将这些值插入另一个函数来制作图表,否则整个图表将在每次函数时继续呈现更新。我无法得到函数之外的函数我在stackoverflow找到了这个答案,但据我所知,大多数答案都使用函数内部的数据。
如何在JavaScript函数之外获取更新值并将其插入另一个函数?或者可能有另一种更好的方法来解决这个问题?
我的代码:
function getData(){
var data;
$http.get(path).then(function(response){
data = response.data
console.log(response.data) //works fine
})
return data
}
console.log(data)//returns undefined
现在如果我想用$ timeout更新函数
我需要做这样的事情
$timeout(getData, 3000)
现在我如何获得这些更新的值?
编辑:
var myData = setInterval( function(){
$http.get(path).then(function(response){
return response.data['Block Devices']
})
}, 1000 );
console.log(myData)
});
返回14我不知道那是什么,但肯定不是正确的数据
答案 0 :(得分:2)
使用$ q of angular,你需要注入$ q作为依赖,
function getData(){
var deffered = $q.defer();
var data;
$http.get(path).then(function(response){
data = response.data
$q.resolve(data);
})
return deffered.promise;
}
var getDataCall = getData();
getDataCall.then(function(data){
console.log(data);
})
如果您想重复此次通话,请使用$interval(fn, delay, [count], [invokeApply], [Pass]);
请参阅文档https://docs.angularjs.org/api/ng/service/$interval
试试这个重复,(我还没试过),
function getData(){
var deffered = $q.defer();
var data;
$http.get(path).then(function(response){
data = response.data
$q.resolve(data);
})
return deffered.promise;
}
$interval(function(){
getData().then(function(data){
console.log(data);
});
},3000);
答案 1 :(得分:1)
您只需要在一个时间间隔内运行代码:
setInterval( function(){
$http.get(path).then(function(response){
console.log(response.data);
// Update your page here
});
}, 3000 );
答案 2 :(得分:1)
您希望使用Angular的$interval
模块执行此操作。
基本上,您有一个名为getData
的函数,它使用$http
模块。然后,在该承诺的解决方案中,您使用新数据设置$scope.chartData
。
然后在一段时间内调用该函数:
$interval($scope.getData, 3000); // every 3 seconds
如果您在控制器中执行此操作,则不需要$q
,但您应该将其从控制器中取出并将其放入服务中,然后使用$q
模块。
Here is a CodePen on HTTP polling in Angular使用完整的Angular应用作为与您提供的端点配合使用的解决方案。
更新:如果您想从控制器外部的外部功能调用它,可以通过创建使用$q
并将其注入控制器的工厂来实现。相同的例子,但有一个工厂:CodePen on HTTP polling in Angular (using a factory)
您还会在示例中注意工厂/服务中的$scope.getBytesWritten
方法。您需要执行一个函数才能获取部分数据 - 只需尝试访问图表数据的属性即可更新该单个值。请参阅CodePen on HTTP polling in Angular (using a factory)。
答案 3 :(得分:0)
<强>您好,强>
在AngularJS中, 有一项名为 $ timeout 的服务,它可以在一个循环中完成您正在寻找的。
在此处查看完整的官方文档:$timeout service documentation
答案 4 :(得分:0)
$interval
服务与$http
服务 $http
服务会返回您可以用于链接的承诺。使用$interval
服务从该承诺中重新创建新承诺和链,以将更新后的数据放在范围内。
返回该承诺和链。
angular.module("myApp").controller("myVm",
function($scope,$http,$interval) {
var count = 0;
function getHttpPromise(url) {
var promise = $http.post(url);
//return for chaining
return promise;
};
function updateScope(promise) {
//chain from promise
promise.then (function (response) {
count++;
$scope.currentCount = "Current response "+count;
});
};
updateScope(getHttpPromise("/echo/json"));
var intervalPromise = $interval( function(){
updateScope(getHttpPromise("/echo/json"));
}, 1000 );
$scope.onStopUpdate = function () {
$interval.cancel(intervalPromise);
});
});
以上示例的函数getHttpPromise
返回 链接的承诺。来自httpPromise的updateScope
函数链来更新范围。然后,该示例使用$interval
服务每1000毫秒更新一次范围。
$interval
服务还会返回可用于链接和取消的承诺。