[英]How to keep calling a function when scope variable changes?
我有一个看起来像的控制器
.controller('GraphsController', function($scope, TemperaturePoller, SoundPoller){
$scope.temperatures = {readings: [], dateTimes: []}
$scope.temperatures = TemperaturePoller.data;
$scope.$watch(function(scope) {return scope.temperatures},
function(newValue, oldValue) {
console.log("Value Changed: " + newValue);
$scope.graph(newValue);
})
$scope.graph = function(data) {
console.log('values changes, refreshing graph');
}
})
并且每10秒从Server
检索一次该值
.factory('TemperaturePoller', function($http, $timeout){
var data = {};
var poller = function() {
$http.get('http://localhost:8080/monitoring/rest/graph/temperature').then(function(r){
data = extractTemperatureReadings(r.data)
data.calls++;
$timeout(poller, 10000)
});
}
poller();
return {
data: data
}
})
我想$scope.graph
要每次调用$scope.temperature
变化
我在浏览器console.log
看到它只被调用了一次
值更改:[对象对象] monitoring.js:19个值更改,刷新了图形
自从我知道API中的数据正在更改后,如何强制每10 seconds
调用一次?
由于您正在查看的数据是一个对象,因此请将$watch
函数上的objectEquality
标志设置为true
。 这是第三个参数。
$scope.$watch('temperatures', function(newValue, oldValue) {
console.log("Value Changed: " + newValue);
$scope.graph(newValue);
}, true);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.