我想在我的服务器控制器中对服务器进行一些更改。我举了一些例子来解释我的问题。递归方法“更新”似乎不会更新$scope.gdevPiechartArchiveVolume
的值。有人能告诉我为什么吗?有没有办法触发更新?谢谢!
angular.module('socketTestApp')
.controller('MainCtrl', function($scope, Socket) {
Socket.on('gdevAreachart_archiveVolume_update', function(data_) {
selectArchiveData(data_);
});
function selectArchiveData(data) {
var i = 0;
var selectedData;
$scope.update = function() {
_.delay($scope.update, 2000);
if ($scope.pause)
return
i++
$scope.gdevPiechartArchiveVolume = i; //<--- this seems not to work but only if i set $scope.pause true
console.log('Updates in $scope.update: ' + $scope.gdevPiechartArchiveVolume);
}
$scope.update();
}
我的直接(观看的变量数据为“$scope.gdevPiechartArchiveVolume
”):
angular.module('socketTestApp')
.directive('gdevAreachart', function() {
var linker = function(scope, element, attr) {
scope.$watch('data', function(newVals, oldVals) {
console.log("Direktive: " + newVals);
return //make some other thinks
}, true);
控制台输出(每次都应打印“Direktive:
...”,例如“$ scope.update:
中的更新”:
Direktive: undefined gdevAreachart.js:14
Updates in $scope.update: 1 main.js:127
Direktive: 1 gdevAreachart.js:14 <---- set scope.pause = true ... false to continue
Updates in $scope.update: 2 main.js:127
Updates in $scope.update: 3 main.js:127
Updates in $scope.update: 4 main.js:127
Updates in $scope.update: 5 main.js:127
Updates in $scope.update: 6 main.js:127
Direktive: 6 gdevAreachart.js:14 <---- set scope.pause = true ... false to continue
Updates in $scope.update: 7 main.js:127
Updates in $scope.update: 8 main.js:127
Direktive: 8 gdevAreachart.js:14 <---- set scope.pause = true ... false to continue
Updates in $scope.update: 9 main.js:127
Updates in $scope.update: 10 main.js:127
Direktive: 10 gdevAreachart.js:14 <---- set scope.pause = true ... false to continue
Updates in $scope.update: 11 main.js:127
Updates in $scope.update: 12
解决方案:
function selectArchiveData(data) {
var i = 0;
var selectedData;
$scope.update = function() {
if ($scope.pause)
return
i++
$scope.gdevPiechartArchiveVolume = i;
console.log('Updates in $scope.update: ' + $scope.gdevPiechartArchiveVolume);
setTimeout(function() {
$scope.$apply($scope.update());
}, 2000);
}
$scope.update();
}
答案 0 :(得分:0)
您是否尝试过使用$ scope。$ apply()? 每次想要从角度“不关心”的函数更新角度$ scope时,你需要这样做,在你的情况下Socket.on