Adding a safe $scope.$apply() method or a queue for $scope.$digest() #3782
Comments
Note that this can still happen with events triggered by code, though. If that's a possibility in your case, you should guard your |
Use |
$scope.$evalAsync doesn't update the values for me, I have to do $scope.$apply . My request stands, please change $scope.$apply to work in queues rather than throwing up the error. |
Can you make a demo? If you call // let's say there is an $apply (or a $digest) in progress
$scope.$apply(function () {
// you code somewhere here
$scope.$evalAsync(function () {
$scope.x = 4;
});
// end of your code
}); If you have a |
I didn't realize that |
It does not need to be called inside Just call |
I have just tested this with the following code:
And in this case the |
Oh, I see. I think you are making the mistake of calling $el.on('switch-change', function(ev, data) {
var state = data.value;
scope.$evalAsync(function() {
scope.model = scope.bsSwitch.getValue(state);
});
});
|
This is what I'm now trying: And it has no effect either, |
I see, you are having this issue right? http://plnkr.co/edit/2kqGi5LQoS51j3UduGCO?p=preview This has actually been fixed in the master already and it is scheduled for the next release (1.2-rc2). This is the commit: 6b91aa0#L0R686 Here is the same demo using the latest angular build: http://plnkr.co/edit/WGBioumFO3qpqK7eRXlZ?p=preview |
Lodash can do the job : https://lodash.com/docs#debounce. |
Please make
$scope.$digest()
work in a queue fashion, i.e if there are multiple calls for it, just process them in a queue rather than throw an error that an existing digest is running.For example, I have a jquery plugin set up which is external to angularjs. Each time the user clicks a button produced by the plugin, it causes a variable to change, and in the plugin's
on('plugin-change')
method, I do$scope.$apply()
to push the variable change.However, what if a user clicks the button too quickly? While so far the digest runs fine in such cases, I'm concerned that if my scope grows big enough and a user clicks fast enough, this will cause the 'digest already running' error.
The text was updated successfully, but these errors were encountered: