A
A
Artem Shchurin2016-01-20 09:58:21
Angular
Artem Shchurin, 2016-01-20 09:58:21

Why doesn't data-binding work, but when $scope.digets() is called, it says apply is already running?

Good day!
I am finalizing the history mode, there is a slider that can be launched and the data on the elements will begin to be updated at a given interval.
$scope.timeline.range.start(end) - model variables, show the current displayed range
_intermediateRange.start(end) - used to send requests for data, when initializing the history mode, I pass an object to the request sending mechanism
The request sending mechanism itself was written even before angular appeared on the project
Why did I need an additional object (_intermediateRange)?
The increment of the model variable occurs immediately after receiving the response, as a result of which the data in the model changes before the request leaves, it is not possible to change the mechanism for sending data
So, at each iteration, we get into _playback, where we change the model variable, and here the data-binding problem does not work, requests go away, $ scope.timeline.range.start (end) changes, but the old data remains in the view, added $scope.digest(); 3rd line of the _playback method and I get in the console:
Error: [$rootScope:inprog] $apply already in progress
I understand that digest is already in progress

function _playback(sender, eventArgs) {
        $scope.timeline.range.start = _intermediateRange.start;
        $scope.timeline.range.end = _intermediateRange.end;
        $scope.$digest();
        _playStep();
      }
function _playStep() {
        var range = parseInt($scope.timeline.range.end - $scope.timeline.range.start);

        if(_intermediateRange.end == $scope.timeline.bounds.end) {
          return false;
        }else if(_intermediateRange.end + $scope.timeline.shift > $scope.timeline.bounds.end) {
          _intermediateRange.end = $scope.timeline.bounds.end;
          _intermediateRange.start = $scope.timeline.bounds.end - range;
        }else {
          _intermediateRange.start += $scope.timeline.shift;
          _intermediateRange.end += $scope.timeline.shift;
        }
      }

And this whole interesting story began with data-binding after I added the _playback call to the next method, you can do without it and there will be no actual errors)
But then the request for the first portion of data will be the one that we already see
function _startPlayback() {
        var timeline = $scope.timeline;
        var rate = timeline.updateRate / timeline.multiply;
        rate = rate < MIN_RATE ? MIN_RATE : rate;

        _stopPlayback();

        if($scope.timeline.range.end > $scope.timeline.bounds.end) {
          _setToStart();
        }

        _playback();
        params.makeAction($scope.controller, 'timeline_start_playback', {_playback: _playback, rate: rate});
      }

Please tell me who knows how to debug angular, understand who called didgest and why the data in the model remains unchanged when the $scope variable changes.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Artem Shchurin, 2016-01-20
@schurin

$scope.$$phase || $scope.$digest()
Solved the problem, before calling $digest we check if digest($apply) is already running It
still remains unclear why everything was unchanged in the view

L
lega, 2016-01-20
@lega

where we change the model variable, here the data-binding problem does not work
What do you mean by "model"?
$scope.digets() call says apply is already running
The order is this, after some event, the angular (or you) call $digest, it looks for changes and calls your callbacks, in these callbacks you recursively try to call $digest, so it swears. You need to check the status and don't run $digest if it's running, after that (after your callback) Angular does another digest loop and finds your changes from the last callback.
data-binding not working
If the data is not displayed in the DOM, then most likely you are changing the wrong data (or trying to display the wrong ones)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question