How to update angular scope

I want to make some changes to my data controller that I get from the server. I gave an example to explain my problem. The recursive update method does not seem to update the value $scope.gdevPiechartArchiveVolume. Can someone tell me why? Is there a way to initiate an update? Thank!

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();

        }

My direcitve (observed variable data " $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);

Console output (" Direktive:..." should be printed every time, for example, "Updates in $ 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 

Decision:

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
source share
1 answer

$scope. $apply()? , angular $ , "w500 > ", Socket.on

0

Source: https://habr.com/ru/post/1665321/


All Articles