Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to update angular scope
    text
    copied!<p>i want to make some changes in my controller of the data i get from the server. I made an example of these to explain my problem. The recursive method "update" seems not to update the value of the <code>$scope.gdevPiechartArchiveVolume</code>. Could someone tell me why? Is there a way to trigger the update? Thank you!</p> <pre><code>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; //&lt;--- this seems not to work but only if i set $scope.pause true console.log('Updates in $scope.update: ' + $scope.gdevPiechartArchiveVolume); } $scope.update(); } </code></pre> <p>My direcitve (the watched variable data is "<code>$scope.gdevPiechartArchiveVolume</code>"):</p> <pre><code>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); </code></pre> <p>Console output (the "<code>Direktive:</code>..." should be printed every time like "Updates in $<code>scope.update:</code>"):</p> <pre><code>Direktive: undefined gdevAreachart.js:14 Updates in $scope.update: 1 main.js:127 Direktive: 1 gdevAreachart.js:14 &lt;---- 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 &lt;---- 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 &lt;---- 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 &lt;---- set scope.pause = true ... false to continue Updates in $scope.update: 11 main.js:127 Updates in $scope.update: 12 </code></pre> <p>Solution:</p> <pre><code>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(); } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload